如何让敌人在统一c#中从屏幕的顶部移动到底部?

时间:2015-09-27 06:52:54

标签: c# unity3d

我正在尝试使用统一创建2D太空射击游戏类型,但我似乎无法让衍生的敌人从屏幕的顶部移动到底部,我实际上是团结和c#的新手,我无法弄清楚我的代码有什么问题。

这是我的代码:

using UnityEngine;
using System.Collections;

public class EnemyControl : MonoBehaviour {

float speed;
// Use this for initialization
void Start () {
    speed = 2f;
}

// Update is called once per frame
void Update () {
    Vector2 position = new transform.position;

    position = new Vector2(position.x, position.y - speed * Time.deltaTime);

    transform.position = position;

    Vector2 min = Camera.main.ViewportToWorldPoint(new Vector2(0, 0));

    if(transform.position.y < min.y) {
    Destroy(gameObject);
    }
}

1 个答案:

答案 0 :(得分:4)

这是你应该如何将对象从上到下移动

using UnityEngine;
using System.Collections;

public class EnemyControl : MonoBehaviour {
   float speed;

    void Start () {
      speed = 2.0f;
}

// Update is called once per frame
void FixedUpdate () {

    godown();

}

void godown() {
    transform.position += Vector3.down *speed* Time.deltaTime;
}

现在在游戏世界中添加另一个对象,你可以称之为驱逐舰。将新对象放在场景的底部。给一个落下的对象一个标签名称&#34; fallingObject&#34;。并使用新对象添加此脚本。

using UnityEngine;
using System.Collections;

public class DestroyCubes : MonoBehaviour
{
void OnCollisionEnter2D(Collision2D col)
{
    if(col.gameObject.name == "fallingObject")
    {
        Destroy(col.gameObject);
    }
}
}

新脚本名称应为&#34; DestroyCubes&#34;