【问题标题】:Unity 2D - Why can not the enemy made?Unity 2D - 为什么不能制造敌人?
【发布时间】:2016-11-10 20:31:50
【问题描述】:

我正在制作一款跑步游戏,但我面临着一个大问题。 在这个阶段,障碍物被创造出来。但一段时间后,团结并没有成为新的障碍。为什么会这样?

public class GameManager : MonoBehaviour {
    public float waitingTime = 1.5f;
    public static GameManager manager;
    public bool ready = true;
    public GameObject cactus;
    float time = 0;

    // Use this for initialization
    void Start () {
        manager = this;
    }

    // Update is called once per frame
    void Update () {
        time += Time.deltaTime;
        //Debug.Log(time);
        if(time>2f && ready==true)
        {
            ready = false;
            time = 0;
            InvokeRepeating("MakeCactus", 1f, waitingTime);
        }
    }
    void MakeCactus()
    {
        Instantiate(cactus);
    }
   public void GameOver()
    {
        //CancelInvoke("MakeCactus");
        iTween.ShakePosition(Camera.main.gameObject, iTween.Hash("x", 0.2, "y", 0.2, "time", 0.5f));
    }
}

【问题讨论】:

  • 实例化更新成本高,使用池显示和隐藏预实例化对象

标签: unity3d unity5


【解决方案1】:

您根本不需要 Update 方法。当您使用它只是为了延迟产卵时。你的代码可以这样重写:

public class GameManager : MonoBehaviour 
{
    public float waitingTime = 1.5f;
    public static GameManager manager;
    public GameObject cactus;

    void Awake() 
    {
        manager = this;
        InvokeRepeating("MakeCactus", 3f, waitingTime);
    }

    void MakeCactus()
    {
        Instantiate(cactus);
    }

    public void GameOver()
    {
        //CancelInvoke("MakeCactus");
        iTween.ShakePosition(Camera.main.gameObject, iTween.Hash("x", 0.2, "y", 0.2, "time", 0.5f));
    }
}

希望这能解决问题

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多