Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by TaranSaini2005 · Feb 27, 2017 at 11:12 AM · animationartificial intelligencezombies

Multiple enemy AI using the same animations/animation controller

Creating a Call of Duty style zombie game. all zombies that spawn have the same animation controller and have three animations which are walking, attacking and falling/death. during testing with one zombie everything was working fine. when i then starting to spawn multiple zombies i found that only one of the zombies each wave would animate properly and the rest would only do the walking animation but neither the attacking or falling. here is the zombie manager script that is on every zombie that spawns, would be a great help if anyone could point me towards the best/correct way of resolving this issue:

 public class ZombieManager : MonoBehaviour 
 {
     Transform playerPos;    // the players position  
     NavMeshAgent agent;            
     public int zombieHealth;            // health of each zombie
     public float sinkSpeed = 0.01f;        // The speed at which the enemy sinks through the floor when dead.
     static Animator zombieAnimator;        // gain access to all animation for the zombies 
 
     // Use this for initialization
     void Start () 
     {
         agent = GetComponent<NavMeshAgent>();    
         zombieAnimator = GetComponent<Animator> ();
         playerPos = GameObject.FindGameObjectWithTag ("Player").transform;    // get the players current position 
     }
     
     // Update is called once per frame
     void Update () 
     {
         if (zombieHealth > 0) // we only want to do stuff if the zombie is still alive
         {
             agent.SetDestination (playerPos.position);    // have the zombie chase the player 
 
             if (Vector3.Distance (playerPos.position, this.transform.position) < 2)    // the zombie can only attack if the player is within 2 units 
             {
                 zombieAnimator.SetBool ("isAttacking", true);
                 zombieAnimator.SetBool ("isWalking", false);
             } 
             else   // if the zombie is not attacking then make it use the walking animation 
             {
                 zombieAnimator.SetBool ("isAttacking", false);
                 zombieAnimator.SetBool ("isWalking", true);
             }
         }
 
         else if (zombieHealth <= 0)        // if the zombie no longer has any health the we can enter the DEATH state 
         {
             ZombieDeath ();
         }
     }
 
     public void ZombieDeath()
     {
         // turing all the other animations off and the falling one on 
         zombieAnimator.SetBool ("isAttacking", false);
         zombieAnimator.SetBool ("isWalking", false);
         zombieAnimator.SetBool ("isFalling", true);
 
         GetComponent <UnityEngine.AI.NavMeshAgent> ().enabled = false;        // Find and disable the Nav Mesh Agent.
 
         GetComponent <Rigidbody> ().isKinematic = true;        // Find the rigidbody component and make it kinematic (since we use Translate to sink the enemy).
 
         StartCoroutine (zombieSinking()); // now that the zombie is DEAD, instead of just destroying it straight away we will have it sink beneath the ground 
     }
 
     public IEnumerator zombieSinking()
     {
         yield return new WaitForSeconds(3f);
 
         // ... move the enemy down by the sinkSpeed per second.
         transform.Translate (-Vector3.up * Time.deltaTime/sinkSpeed);
 
         yield return new WaitForSeconds(3f);
 
         Destroy (gameObject);
     }
 }



Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
4
Best Answer

Answer by Commoble · Mar 02, 2017 at 04:28 AM

When you have a static variable in a class, that variable belongs to the class itself, not individual instances of that class. Your zombieAnimator is static, so you only have one zombieAnimator field being shared by all of your ZombieManager instances. However, since each individual zombie has their own Animator component (and their own ZombieManager), you don't want to make this variable static.

Further down, you have zombieAnimator = GetComponent<Animator> (); in your Start() event. What this is doing is: when each of your zombies initializes when the game starts, they're taking the global animator variable, and assigning their own animator component to it, overriding the previous zombie's animator. At the end of the line, zombieAnimator will be set to the last zombie in line's -- meaning each zombie's ZombieManager will now have a reference to that Last Zombie In Line's animator, but not their own.

Further down, in your update function, your individual zombies are each setting the properties of that one Last Zombie In Line's animator, while ignoring their own animator component.

You can fix this by changing static Animator zombieAnimator to public Animator zombieAnimator and linking the zombieAnimator field to the Animator component in your zombies' prefab.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image stewe02 · Apr 10, 2020 at 04:23 PM 0
Share

Hello , I'm having the same problem but I've been using the animator state behaveours ins$$anonymous$$d. Is there any way I can fix this ? Pls help me , I've already spent several days on making the AI and I just realised it won't work on multiple enemies !

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Navmesh good looking animations 0 Answers

Can the animation editor create local rotational data? 3 Answers

Adding animation clips via script 2 Answers

Mesh collider on multiple meshed animated character? 0 Answers

Zombies AI 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges