不知道我在这里做错了什么,我试图在unityEdit中触发一个动画:问题不在于敌人在动画播放前被摧毁,因为敌人甚至没有被摧毁,当
玩家.cs
private void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.tag == "Enemy")
{
Blue blue = collision.gameObject.GetComponent<Blue>();
if (action == State.jump || action == State.fall)
{
blue.JumpedOn();
blue.Death();
jumpVelocity = multiplier * jumpVelocity;
rb.velocity = Vector2.up * jumpVelocity;
}
}
}
敌人.cs
public void JumpedOn(){
action = State.death;
anim.SetBool("Death", true);
}
public void Death() {
Destroy(this.gameObject);
}
在动画窗口中也设置了条件,如果死亡=true,播放动画。见此
当我把
blue.JumpedOn();
其他线路将正确运行
blue.Death();
jumpVelocity = multiplier * jumpVelocity;
rb.velocity = Vector2.up * jumpVelocity;
我想你应该尝试在 blue.JumpedOn();
和 blue.Death();
.
你需要在JumpedOn()和Death()之间设置一个延迟,因为你在同一帧中调用它们,对象在动画播放之前就会被破坏。一个很好的方法就是coroutine,它对于延迟代码的执行非常有价值。
private IEnumerator coroutine;
IEnumerator WaitAndDestroy(GameObject _callingObject, float _waitTime)
{
yield return new WaitForSeconds(_waitTime);
_callingObject.Destroy();
}
public void Death() {
coroutine = WaitAndDestroy(gameObject, 1.5f);
StartCoroutine(coroutine);
}