Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Correct Animation Plays Only On First Instance

Discussion in 'Animation' started by g-hoot, Nov 28, 2016.

  1. g-hoot

    g-hoot

    Joined:
    Apr 18, 2015
    Posts:
    69
    Guys, I'm working on a VR game, and I am having issues with animations. I have some enemies that are activated once my player gets within a certain range of them. For some reason, the correct animation only plays on the first one that is activated. The idle animation plays on the others. I have a bool set up in the animator to set to true if the object is moving. I used Behaviour Designer to do the movement stuff, but I don't "think" it has anything to do with this issue. For some reason, the animator is only "initialized" on the first enemy activated.

    The Enemies are instantiated when the scene loads, and I have the character in an empty game object, and also have moved the animator to the empty game object too, so that it already exists before the character is enabled, but still get the same behavior.

    Any ideas?
     
  2. siskyn

    siskyn

    Joined:
    Jan 27, 2017
    Posts:
    2
    I have the exact same problem.

    I made a prefab for a npc that uses script for AI and setting animation boolean variables, and a Animator Controller for running animations based on the bools inside. It's working like a charm for the first instance.
    But every other instance of the npc is stuck on a permanent idle loop (the entry animation).

    On further tests, it seems that if I set the animator booleans to other values from the editor, animations will trigger correctly. This means that the booleans are actually not getting changed within the script.

    Immediately I thought it's a issue with the way I retrieve the animator instance in the script, but testing showed that is working well. Each NPC's animator returns a different instanceID.

    More tests, and it looks like the booleans are set corectly at runtime, but the animator does not react to them.

    To summarize, setting the bool variables in editor before starting game, makes the animator switch animations correctly. But setting the bool variables at runtime works only for the first instance of the prefab, and for the rest the animator ignores them and acts as if they are still at default values.

    It looks to me like a unity engine bug. Anyone have further insight into this ? It's a pretty big issue front and center which should affect just about everyone, so I can't shake the feeling I must be missing something.

    To get the animator instance I tried these 2 ways, both work.
    Code (csharp):
    1. anim = GetComponent<Animator>();
    2. anim = this.GetComponent(typeof(Animator)) as Animator;
    To change the active animation I use:

    Code (csharp):
    1. void SetRunning()
    2.     {
    3.         anim.SetBool("isRunning", true);
    4.         anim.SetBool("isIdle", false);
    5.         anim.SetBool("isAttacking", false);
    6.     }
    7.  
    8. void SetIdle()
    9.     {
    10.         anim.SetBool("isRunning", false);
    11.         anim.SetBool("isIdle", true);
    12.         anim.SetBool("isAttacking", false);
    13.     }
     
    Last edited: Jan 27, 2017
  3. g-hoot

    g-hoot

    Joined:
    Apr 18, 2015
    Posts:
    69
    Hey siskyn,
    I never figured it out. There were a few fixes here on the forums, but none of them worked for me. Really frustrating! Was really excited about my game, but killing a month of my spare time jacking with stuff like this sure takes the excitement out of it. LOL. Haven't got back to it for a while now. Good luck to you, and if you get it, please let me know!
     
  4. siskyn

    siskyn

    Joined:
    Jan 27, 2017
    Posts:
    2
    I found not so much a fix, as a work-around.

    Turns out, that the NPCs get bugged if they are instantiated from the editor.
    However, if they are instantiated at runtime, they work correctly.

    So, if you need a fixed number of NPCs at certain locations to spawn, simply place empty game objects at that location, and use them as spawn points in a spawn manager script. Similar to how you would create infinite NPCs from fixed spawn points as done in the Survival Shooter Tutorial - 9 of 10 : Spawning Enemies - Unity Official Tutorial.

    Edit: Gah. It turns out that animations break when having more than 3-4 instances. I wonder if this is a intentional limitation of free unity.
     
    Last edited: Jan 27, 2017
  5. DavidGeoffroy

    DavidGeoffroy

    Unity Technologies

    Joined:
    Sep 9, 2014
    Posts:
    542
    There's no limitation on Unity free, and there should be no reason for animations to break for multiple instances.
    Did either one of you file a bug?
     
  6. g-hoot

    g-hoot

    Joined:
    Apr 18, 2015
    Posts:
    69
    No sir, never filed a bug. There are several post with people having the same issue, and several solutions. Just never got any of them working for me. Figured it is just something I'm doing wrong since I'm a newbie.
     
  7. DavidGeoffroy

    DavidGeoffroy

    Unity Technologies

    Joined:
    Sep 9, 2014
    Posts:
    542
    That's not impossible, but without files to look at, we kinda have to assume the same thing.

    If you file a bug, a QA will triage your bug, maybe tell you what you're doing wrong, or forward the bug to us (the developers), where we'll investigate the bug, fix it, or tell you what you're doing wrong and maybe add safeguards to tell you that you're doing it wrong in the future.

    It's a much more reliable way to get answers or get your bugs fixed.
     
  8. mertsonmez

    mertsonmez

    Joined:
    Feb 8, 2021
    Posts:
    1
    Hello guys,

    Any answers for this subject yet? I`m having the same problem