You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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 Death animation 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:

  1. First, ensure you have a reference to the Animator component (if you don't already):
    private Animator _animator;
    
    private void Awake()
    {
        _animator = GetComponent<Animator>();
    }
    
  2. 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");
    }
    
  3. Modify your OnCollisionEnter2D method 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 Death in 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 isDead boolean 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 Death clip (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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.22 09:59:52