投掷后生成

2023-12-19

我正在从事一个足球比赛项目。我想要一个球体,在抛出第一个球体后,我必须生成另一个球体。这是我尝试过的:

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;
        }
    }
} 
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

投掷后生成 的相关文章

随机推荐