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

Mecanim: Spawn character in specific state?

Discussion in 'Editor & General Support' started by bluescrn, Apr 24, 2013.

  1. bluescrn

    bluescrn

    Joined:
    Feb 25, 2013
    Posts:
    628
    Hi,

    Is there a way to force a Mecanim-controlled character into a specific state on instantiation, so that the first frame rendered will be in in this state?

    I'm attempting to switch to the required animation with animator.setBool, and a transition from 'any state' - but I'm currently seeing a one-frame 'pop' of what appears to be the idle state before it enter the desired state. I can't see any obvious problems with my animator setup, it's a non-atomic transition with a zero-length blend.

    Is this a common problem, and is there a clean solution to it?

    Edit: After a bit of debugging, the root of the problem seems to be that the newly-spawned object is being rendered before a first Update(). I tried doing the setBool() immediately after spawning my object, but it doesn't seem like there's a Mecanim update happening between then and the first render?
     
    Last edited: Apr 24, 2013
  2. bluescrn

    bluescrn

    Joined:
    Feb 25, 2013
    Posts:
    628
    A few months later I've run into this again... and my first search for an answer brings up my old question :(

    This seems an incredibly obvious thing to need to do. For example, you may want to spawn an enemy in a basic idle state, or in a crouched 'jump out of dark corner' state.

    Is there no way to just *force* Mecanim into a starting state, without it having to transition its way through the state machine? Seems a rather important feature to be missing, and a very ugly one to work around...

    Am I going to have to duplicate the entire Animator (and enemy prefab) just to set a different default state?! Or is there a more sensible approach?

    (Are many people actually using Mecanim in real projects? - from the lack of discussion of the subject, I'm getting the impression that most developers must be sticking with the old animation system?)
     
  3. Archania

    Archania

    Joined:
    Aug 27, 2010
    Posts:
    1,662
    Should be the initial state be the one you want? Like using idle as the base state for your starting state for the animator and everything get pulled off that to your different states?
     
  4. bluescrn

    bluescrn

    Joined:
    Feb 25, 2013
    Posts:
    628
    But what do you do if you need different instances of a character to have different initial states?

    I might want an enemy to start in a sitting pose (e.g. in a vehicle), and others to start in a regular idle/walk pose. But beyond the initial 'spawn animation' they both behave the same.

    I don't want to have to duplicate the Animator (and perhaps the enemy prefab) just to change this initial animation state. I want to do it from a script when instantiating the object.
     
  5. Archania

    Archania

    Joined:
    Aug 27, 2010
    Posts:
    1,662
    Not sure if this will work...
    Pull off from the any state the different starting you want and have a bool that switches based on the enemy you are starting. You know what I mean?
    Might work...
     
  6. bluescrn

    bluescrn

    Joined:
    Feb 25, 2013
    Posts:
    628
    That's the best that I've managed so far. I've not managed to avoid a one 1-frame glitch, though (one frame in the default state before moving to the desired state).

    Also, using bools to change state from the Any State state can get a little bit ugly - as you have to turn the bool back off immediately upon entering the state - but not before - to avoid it repeatedly entering the state from itself...

    So a function to force Mecanim immediately into a specific state is still quite desirable...
     
    Claytonious likes this.
  7. Archania

    Archania

    Joined:
    Aug 27, 2010
    Posts:
    1,662
    I can't think of a different way to do it atm. You kinda want what the legacy system does to start out with.
    Glad you have something though. Not the answer you where looking for though I'm sure.
     
  8. UnLogick

    UnLogick

    Joined:
    Jun 11, 2011
    Posts:
    1,745
    You're quite right this is currently unsupported, the only way to achieve what you're looking for is to spawn it a frame early and have it hidden so people wont see the 1 frame glitch. If this is your desired workflow, use a default state that have transitions to the spawning states you're interested in. I recommend using an int parameter, and have one "reset" bool that leads from any state back to this default state.

    Please note that Mecanim is still in active development and the goal is to have it fully replace the legacy system. Fully replacing legacy also includes the functionality that you're requesting.
     
  9. Jmonroe

    Jmonroe

    Joined:
    Jul 7, 2012
    Posts:
    118
    My initial state is actually a blend tree with 3 different idle clips that don't really blend. I use the blend float parameter to set the initial animation. Its a little hacky, but another thing to try.
     
  10. ImpossibleRobert

    ImpossibleRobert

    Joined:
    Oct 10, 2013
    Posts:
    511
    I know the thread is quite old but I have a similar situatiuon now.

    • What is the currently recommended way to spawn characters in different states from a common animator?
    • And similarly importantly, is there a way to force a character into a pose inside the editor before entering playmode? I want to put one character into a chair and have a sit animation but it is rather tedious to try to move the T-Pose character pixel by pixel, hit play to see if the sitting animation fits and then repeat. I would like to nicely layout everything outside playmode already.
     
    Claytonious likes this.
  11. Claytonious

    Claytonious

    Joined:
    Feb 16, 2009
    Posts:
    881
    I'm still facing the 1-frame glitch when I need an object to start in some state other than the default state of the state machine, and I don't want to duplicate the entire state machine just to choose a new default state for some objects vs. others. Has there been any progress since this old question was asked?
     
  12. AShim-3D

    AShim-3D

    Joined:
    Jul 13, 2012
    Posts:
    33
    I did it so:
    - spawn (Instantiate or SetActive) GameObject with Animator component (Mecanim);
    - set all parameters (Animator.SetBool etc.);
    - then call Animator.Update(0f) or Animator.Update(0.0001f).
    Result: no 1-frame glitch.
     
    eses, hopeful and Claytonious like this.
  13. ImpossibleRobert

    ImpossibleRobert

    Joined:
    Oct 10, 2013
    Posts:
    511
    Does anyone know of a way to activate a certain animation (e.g. sit idle) in the editor to pose him in edit mode, e.g. to let him sit on a chair?
     
  14. Claytonious

    Claytonious

    Joined:
    Feb 16, 2009
    Posts:
    881
    Thanks, AShim, will try that!
     
  15. Claytonious

    Claytonious

    Joined:
    Feb 16, 2009
    Posts:
    881
    Hmmm - since you are doing this, why don't you get the "Animator is not playing a Playable" at step 2 ("set all parameters")? I do. I'm not able to, e.g., set a trigger that will put me into the correct state until the thing is already playing, but it will play 1 frame in the wrong state. Are you getting away with this successfully because you're using SetBool instead of triggers?
     
  16. AShim-3D

    AShim-3D

    Joined:
    Jul 13, 2012
    Posts:
    33
    Take the sample.
     

    Attached Files:

    g-hoot likes this.