【问题标题】:Having trouble with WaitForSecondsWaitForSeconds 遇到问题
【发布时间】:2014-12-11 23:56:29
【问题描述】:

我正在尝试让我的游戏在与道具碰撞时为玩家提供道具。通电功能正常,但是当它到达 yield WaitForSeconds 语句时,执行后什么也没有,导致玩家永远拥有通电,而不是设定的时间。为什么之后的代码没有执行,我该如何解决?

public void OnCollisionEnter2D( Collision2D collision ){
    if (collision.gameObject.tag == "Player") {
        if(isActive){
            isActive = false;
            gameObject.renderer.enabled = false;
            StartCoroutine( activatePowerup( collision.gameObject ) );

        }else;
        Debug.Log ("Hit player");

    }else if (collision.gameObject.tag == "Ground") {
        Destroy(gameObject);
    }

}

public IEnumerator activatePowerup( GameObject player ){
    player.GetComponent<Player>().setAttackDelaySeconds (0.25f);
    Debug.Log ("Powerup Activated");
    yield return new WaitForSeconds(2.0f);  
    Debug.Log ("Finisehd waiting");
    deactivatePowerup ( player );

}

public void deactivatePowerup( GameObject player ){
    player.GetComponent<Player> ().setAttackDelaySeconds (0.75f);
    Debug.Log ("Powerup Finished");
    Destroy (gameObject);
}

【问题讨论】:

    标签: c# unity3d


    【解决方案1】:

    我知道出了什么问题。由于道具在玩家身上激活后会撞到地面,因此在停用道具之前会破坏物体,因此永远不会停用。这已通过更改来解决...

        }else if (collision.gameObject.tag == "Ground") {
            Destroy(gameObject);
        }
    

        }else if (isActive && collision.gameObject.tag == "Ground") {
            Destroy(gameObject);
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-06-21
      • 1970-01-01
      • 2014-11-30
      • 2021-08-09
      • 2017-05-02
      • 2021-02-01
      • 2012-10-31
      • 2019-07-26
      相关资源
      最近更新 更多