c# - throw 后生成

标签 c# unity-game-engine

我正在开发一个足球游戏项目。我想要一个球体,在抛出第一个球体后,我必须生成另一个球体。这是我尝试过的:

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;
        }
    }
} 

关于c# - throw 后生成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40403377/

相关文章:

c# - 将异常或消息从线程/任务传递到主线程

c# - GL.Lines Unity3D (C#) - 线条未在编辑 View 中渲染

ssl - 有没有办法防止Unity中的javax.net.ssl.SSLHandshakeException因为服务器不支持SSLv3来防止poodle攻击?

unity-game-engine - 敌人逻辑错误

azure - 使用 Microsoft Azure Text To Speech with Unity 时,播放声音的开头和结尾会出现断音

c# - 有没有更短的写法 x == y OR x == z?

c# - 防止用户在 Multi-Tenancy 环境中多次投票的最佳方法

c# - 正则表达式匹配两个字符之间的多个数字组

c# - 如果 C# 句子中存在该单词,如何屏蔽句子中的特定单词

c# - Unity - 将 Leped 位置反转回原始位置