我正在做一个足球比赛项目。我想要一个球体,在我投出第一个球体后必须生成另一个球体。这是我尝试过的:
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);
}
}
}
最佳答案
如果最后掷出的球足够远,则只需实例化一个新球。尝试这个:
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;
}
}
}