无论设置备用点如何,产生的单位总是移动到相同的目标

时间:2015-09-19 15:02:50

标签: c# unity3d

我是团结的新手,我正在努力创造一款新游戏。

using UnityEngine;
using System.Collections;

public class Enemyblue1script : MonoBehaviour {

    // Use this for initialization

public float speed; // speed variable
public float RotSpeed = 90f;
public GameObject RocketGO; // Reference to our main character Rocket
void Start ()
{}

void Update()
{
    Vector2 Pos = new Vector2 (-10, 5);
    gameObject.transform.position = Vector3.MoveTowards (gameObject.transform.position, Pos, speed * Time.deltaTime);
    Invoke ("MoveFace", 2f);
}

void MoveFace()
{
    if( RocketGO == null)
    {
        GameObject go = GameObject.FindGameObjectWithTag("Rocket");
        if(go != null){
            RocketGO = go ;}
    }
    if( RocketGO == null)
        return;

    Vector3 dir = RocketGO.transform.position - gameObject.transform.position;
    dir.Normalize();

    float zAngle = Mathf.Atan2(dir.x , dir.y) * Mathf.Rad2Deg + 360;

    Quaternion desiredRot = Quaternion.Euler(0 ,0,-zAngle);
    gameObject.transform.rotation = Quaternion.RotateTowards(gameObject.transform.rotation , desiredRot , RotSpeed * Time.deltaTime);
}
}

这是我的移动敌人的脚本(使用MoveTowards功能)并将敌人旋转到玩家方向。我还有一个用于产生这些能量的脚本(一次两个敌人)脚本是

using UnityEngine;
using System.Collections;

public class Enemy1Spawner : MonoBehaviour {

//our meteorite prefab
public GameObject Enemy1blue;
public float speed;
public GameObject p1;
public GameObject p2;

// Use this for initialization
void Start ()
{}

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

void SpawnEnemy1blue()
{
    //bottom left point of screen
    //Vector2 min = Camera.main.ViewportToWorldPoint (new Vector2 (0, 0));

    //top right point of screen
    //Vector2 max = Camera.main.ViewportToWorldPoint (new Vector2 (1, 1));

    GameObject Enemy1ablue = (GameObject)Instantiate (Enemy1blue);
    GameObject Enemy2ablue = (GameObject)Instantiate (Enemy1blue);
    Enemy1ablue.transform.position = new Vector2 (-11,9);
    Enemy2ablue.transform.position = new Vector2 (11,9);
}

public void startEnemy1blueSpawner()
{
    Debug.Log ("Invoked Spawning Blue Enemy");
    InvokeRepeating ("SpawnEnemy1blue",5f,30f); 
}

//function to stop spawning when game over
public void StopEnemy1blueSpawning()
{
    CancelInvoke ("SpawnEnemy1blue");
}
}

所以现在虽然产生了敌人!我的敌人只朝着一个点Vector2 Pos = new Vector2 (-10, 5);移动,我在Updat()的{​​{1}}方法中定义了Enemyblue1script,我希望在Vector2 Pos = new Vector2 (-10, 5)处的不同位置产生两个敌人(在左侧);和一个Vector2 Pos = new Vector2 (10, 5)(右侧);

通过使用这两个脚本,我能够一次产生两个敌人,但它们都移动到同一点,这是我不想发生的事情。我尝试了ENEMY1SPAWNER脚本>等内容SpawnEnemy1blue()函数我还使用vector2创建了两个点,我在那里为两个不同的敌人调用了两个Movetowards函数但是没有工作它们没有移动。

    Vector2 Pos1 = new Vector2 (-10, 5);
    Vector2 Pos2 = new Vector2 (10, 5);

    GameObject Enemy1ablue = (GameObject)Instantiate (Enemy1blue);
    GameObject Enemy2ablue = (GameObject)Instantiate (Enemy1blue);
    Enemy1ablue.transform.position = new Vector2 (-11,9);
    Enemy2ablue.transform.position = new Vector2 (11,9);

    Enemy1ablue.transform.position = Vector3.MoveTowards (Enemy1ablue.transform.position, Pos1, speed * Time.deltaTime);
    Enemy2ablue.transform.position = Vector3.MoveTowards (Enemy2ablue.transform.position, Pos2, speed * Time.deltaTime);

0 个答案:

没有答案