投掷后产卵

时间:2016-11-03 13:55:14

标签: c# unity3d

我正在开展足球比赛项目。在抛出第一个球体后,我想要一个球体,我必须产生另一个球体。这是我尝试过的东西:

public class spawn : MonoBehaviour {
    public Transform[] SpawnPoints;
    public float SpawnTime;
    public GameObject ball;

    // Use this for initialization
    void Start () {
        InvokeRepeating ("SpawnBalls", SpawnTime, SpawnTime);
    }

    void SpawnBalls(){
        if (transform.position.z > -0.904 ) {
            int SpawnIndex = Random.Range (0, SpawnPoints.Length);
            Instantiate (ball, SpawnPoints [SpawnIndex].position, SpawnPoints [SpawnIndex].rotation);
        }
    }
} 

1 个答案:

答案 0 :(得分:3)

如果投出的最后一个球足够远,只需要实例化一个新球。试试这个:

public class spawn : MonoBehaviour {
    public Transform[] SpawnPoints;
    public GameObject ball;
    public GameObject lastBall;

    // Use this for initialization
    void Start () {
        int SpawnIndex = Random.Range (0, SpawnPoints.Length);
        lastBall = Instantiate (ball, SpawnPoints [SpawnIndex].position, SpawnPoints [SpawnIndex].rotation) as GameObject;
    }

    void Update(){
        if (lastBall.position.z > -0.904 ) {
            int SpawnIndex = Random.Range (0, SpawnPoints.Length);
            lastBall = Instantiate (ball, SpawnPoints [SpawnIndex].position, SpawnPoints [SpawnIndex].rotation) as GameObject;
        }
    }
}