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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Example of Animator.Play function

Discussion in 'Scripting' started by Raybrand, May 22, 2014.

  1. Raybrand

    Raybrand

    Joined:
    Feb 6, 2012
    Posts:
    98
    Hi, Im getting started with this Humanoid animation system and I want to play an animation

    I found Animator.Play in the documentation but all it says is:

    Play(stateName: string, layer: int = -1, normalizedTime: float = float.NegativeInfinity)
    which is pretty vague to me so does anyone have any example code using this?
     
  2. Carpe-Denius

    Carpe-Denius

    Joined:
    May 17, 2013
    Posts:
    842
    Animator.Play("YourLayer.Run");

    You can read the state name in your animator.
     
  3. Raybrand

    Raybrand

    Joined:
    Feb 6, 2012
    Posts:
    98
    I tried typing that line but my animation isn't playing,

    In my animator u can see the layer is just called "Layer" so I typed,
    anim = GetComponent("Animator");// typed at function start
    anim.Play("Layer.NameOfAnimation");

    as you can see in the Animator none of the animations there have any links or transtions between them, with this in mind can u guess why the animation isn't playing?
     

    Attached Files:

  4. Carpe-Denius

    Carpe-Denius

    Joined:
    May 17, 2013
    Posts:
    842
    You don't need transitions for that. Can you clean that a bit? I can't see any animation names or stuff like that ;)
    Just use a second layer if you don't want to change your animations.

    You only need transitions if you want it to jump back to idle when finished, but not for testing now.
     
    Last edited: May 22, 2014
  5. Raybrand

    Raybrand

    Joined:
    Feb 6, 2012
    Posts:
    98
    I looked into the animator window while playing and found that the animation is playing but the idle animation is still playing over it.
    I could see this from the bar progressing with the animation but the character appeared to still be in idle
     

    Attached Files:

  6. sam268

    sam268

    Joined:
    Apr 21, 2014
    Posts:
    149
    can't you just place the animation on the gameobject animation element list, then do Animation.Play("run"); or something?

    Are you having problems with the animator controller mabye?
     
  7. Raybrand

    Raybrand

    Joined:
    Feb 6, 2012
    Posts:
    98
    the object has to be set to legacy in rig settings but I want to try Humanoid animation system, which requires this system
     
  8. Carpe-Denius

    Carpe-Denius

    Joined:
    May 17, 2013
    Posts:
    842
    What are your layer settings? (weight, override or additive)
     
  9. Raybrand

    Raybrand

    Joined:
    Feb 6, 2012
    Posts:
    98
    they where default before, setting the weight or the second layer to 1 allows me to play the animation. what does override or additive do?

    anyway the next step for me is to detect the position of the animation, before I used if(animation.["name of animation"].normalizedTime >= value), how do I do this using animator?
     
  10. Carpe-Denius

    Carpe-Denius

    Joined:
    May 17, 2013
    Posts:
    842
  11. Raybrand

    Raybrand

    Joined:
    Feb 6, 2012
    Posts:
    98
    I need to raise a flag when certain animations reach a point or when it ends, but i need it in code. So how do I use "AnimatorStateInfo.normalizedTime"?
    I saw this line
    AnimatorStateInfo info = animator.GetCurrentAnimatorStateInfo(0);
    Debug.Log(info.normalizedTime);
    how do i write this in javascript?
     
  12. Carpe-Denius

    Carpe-Denius

    Joined:
    May 17, 2013
    Posts:
    842
    I don't know unityscript, sorry.
    I would think something like:

    Code (csharp):
    1.  
    2. var animator:Animator=gameObject.GetComponent(Animator);
    3. var info:AnimatorStateInfo=animator.GetCurrentAnimatorStateInfo(0);
    4. Debug.Log(info.normalizedTime);
     
  13. Raybrand

    Raybrand

    Joined:
    Feb 6, 2012
    Posts:
    98
    not on my computer with unity atm can anyone confirm this

    EDIT: this works sorta, but the normalizedTime stacks up with every animation in the layer, I need this value to be reset to zero when an animation ends or starts. can does someone know how. info.normalizedTime is read only so I can't do something like info.normalizedTime = 0.
     
    Last edited: May 23, 2014