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

Unity僵尸预制体攻击动画失效问题求助(毕业设计)

Fixing Zombie Prefab Attack Animation Issue in Unity FPS

Hey there, I’ve run into this exact kind of problem before with prefabs and static variables—let’s break down what’s going wrong and how to fix it!

The Likely Culprit: Static Animator Variable

Looking at your script, the first red flag is this line:

static Animator anim;

Static variables belong to the class itself, not individual instances of your zombie script. When you turn your zombie into a prefab and spawn multiple instances, every single zombie will share that one static anim reference. That means only the last zombie you spawned will have its Animator correctly assigned, and all others will be using a stale reference—so their attack animations won’t trigger, even though the distance check and damage logic still work (since those don’t rely on the Animator reference being correct for each instance).

Fix Step 1: Remove the Static Modifier

Change the Animator declaration to be an instance variable instead:

Animator anim; // No more static!

Your Start() method already correctly gets the Animator component on the current GameObject, so this will ensure each zombie prefab instance has its own Animator reference.

Additional Checks to Rule Out Other Issues

Even after fixing the static variable, it’s worth verifying these to be safe:

  • Animator Controller Parameter Matching: Double-check that your zombie’s Animator Controller has parameters named exactly isIdle, isWalking, isAttacking, and Death (case-sensitive!). Sometimes when copying animators between models, parameters can get renamed or removed accidentally.
  • State Machine Transitions: Open your Animator Controller and confirm the transition from Idle/Walking to Attack is set to trigger when isAttacking is true. Also make sure there’s no conflicting transition (like a priority that’s preventing the attack state from activating).
  • Prefab Animator Component: Select your zombie prefab in the Project window and ensure the Animator component is attached and pointing to the correct controller. Sometimes prefab instances can lose component references when being updated.

Modified Script Snippet

Here’s the corrected variable declaration and Start method to fix the static issue:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerChaseWithoutFaceFollowing : MonoBehaviour {
    public Transform player;
    Animator anim; // Removed static modifier

    void Start() {
        anim = GetComponent<Animator>(); // Now each instance gets its own Animator
    }

    // Rest of your Update() logic stays the same!
}

Final Notes

Since you mentioned all three zombie models work fine when not using prefabs, the static variable is almost certainly the root cause. Once you remove that static keyword, each prefab instance will control its own Animator independently, and your attack animations should start playing as expected when the zombie gets within 3 units of the player.

内容的提问来源于stack exchange,提问作者Fazil Mahesania

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.27 06:47:27