我正试图让我的游戏在与强力发生碰撞时给予玩家通电。上电功能正常,但是当它达到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);
}
答案 0 :(得分:0)
我弄清楚出了什么问题。由于在播放器上激活后上电将会到达地面,因此在停用上电之前会破坏物体,因此它永远不会停用。这是通过改变......来解决的。
}else if (collision.gameObject.tag == "Ground") {
Destroy(gameObject);
}
到
}else if (isActive && collision.gameObject.tag == "Ground") {
Destroy(gameObject);
}