Unity 2D Animator仅播放动画首帧,死亡动画无法完整播放求助
Fixing Incomplete Death Animation Playback in Unity
Got it, let's tackle this issue where your vampire's death animation doesn't finish before the game over logic runs. The core problem here is that your collision handler is executing the rest of its code immediately, instead of waiting for the animation to wrap up. Here's a straightforward, reliable solution:
Step 1: Add an Animation Event to Your Death Clip
First, we need a way to signal when the death animation has fully played:
- Open your
Vampire Deathanimation clip in the Animation window. - Scroll to the very last frame of the timeline.
- Right-click the timeline and select Add Event.
- In the Inspector for the new event, set the function name to
OnDeathAnimationComplete(you can use any name, just make sure to match it in your script later).
Step 2: Update Your Character Script to Listen for the Event
Next, we'll adjust your script to trigger game over logic only after the animation finishes:
- First, ensure you have a reference to the Animator component (if you don't already):
private Animator _animator; private void Awake() { _animator = GetComponent<Animator>(); } - Add the public callback method that the animation event will trigger:
public void OnDeathAnimationComplete() { // Put all your original game over logic here! // Example: Load game over scene, show UI, stop background audio, etc. Debug.Log("Death animation finished - executing game over logic now"); } - Modify your
OnCollisionEnter2Dmethod to only trigger the death animation (not the game over logic right away):private void OnCollisionEnter2D(Collision2D collision) { if (collision.gameObject.CompareTag("Obstacle")) // Adjust this tag to match your obstacles { // Trigger the death animation _animator.SetBool("isDead", true); // Optional: Disable player movement/collision to prevent extra unwanted interactions GetComponent<PlayerMovement>().enabled = false; // Replace with your movement script name GetComponent<Collider2D>().enabled = false; // DON'T run game over logic here anymore - wait for the animation event! } }
Step 3: Tweak Animator Transition Settings
To make sure the death animation plays all the way through without interruptions:
- Select the transition from Any State to
Vampire Deathin the Animator window. - In the Inspector, set Has Exit Time to
false(so the animation doesn't try to transition out early). - Set Can Transition To Self to
false(prevents unnecessary loops). - Set the transition Duration to
0(for an immediate switch to the death animation).
Quick Checks to Avoid Common Pitfalls
- Double-check that the
isDeadboolean parameter in the Animator exactly matches the string you're using in code ("isDead"). - Confirm the animation event is attached to the final frame of the
Vampire Deathclip (not the start or middle). - If your player has other active scripts (like movement or attack), disable them when triggering death to stop unwanted behavior during the animation.
内容的提问来源于stack exchange,提问作者JayKayKay
相关产品推荐
相关产品推荐

