Search Unity

Animation Activation

Discussion in 'Animation' started by ViolinRobot, Nov 27, 2014.

  1. ViolinRobot

    ViolinRobot

    Joined:
    Nov 27, 2014
    Posts:
    1
    Hey Guys,
    I'm a new Unity Developer, and I'd like to know how to turn on/off a condition for the animator in a script. For instance, I restrict the transition from idle to run only when the "run" condition is true. However, since I have little experience with Unity, I don't know how to make a transition true from within a script. Can you guys help me?

    Violin Robot
     
  2. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,694
    Define your run condition as a Boolean parameter, for example named "CanRun". Add a condition to the transition that requires CanRun==true. Use Animator.SetBool() to set it true or false:
    Code (csharp):
    1. GetComponent<Animator>().SetBool("CanRun", true);
    Better yet, go through Unity's excellent tutorials on animation. They'll serve you very well in this case and many others.

    http://unity3d.com/learn/tutorials/modules/beginner/animation
     
  3. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,697
    I am also new to the animator.
    I am wondering what Speed is in terms of transitions. For instance, using two states, idle and move, for the transitions, idle is true if speed is less than 0.1, move is true if speed is greater than 0.1. However, when my agent is not moving, the move animation is playing??
     
  4. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,694
    Is the Speed parameter > 0.1? If you click on a GameObject that has an Animator component, you can see its runtime state in the Animator window. The active state will have a blue progress bar, and the Parameters section in the lower left will contain the current values of the parameters (and you can even change them). Perhaps Speed is still > 0.1. If this is the case, make sure you call Animator.SetFloat("Speed", 0) when your agent stops. If you're using NavMeshAgent, it doesn't talk directly to Animator. It's up to you to mediate between them.
     
    theANMATOR2b likes this.
  5. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,697
    Ok, if I set the Parameters, Speed, for any state (as if I change it, it is universal), if I put the value at 0.1, the idle animation runs at start. However, if I move, then no change occurs. But you are saying that this value must be changed by me, via code>
     
  6. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,697
    Ok, sorry for that last post,
    So yes. I just got it working. Awesome, thank you. However one issue has arisen. How can I be notified when I tell the player to move to position xyz, but player is blocked, from moving there? I had one issue where this occurred, and as such, player kept animating walk, since he had not reached his destination.


    Thanks!
     
  7. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,694
    You can minimize occurrences by tweaking your NavMeshAgent and/or NavMesh settings. Those settings can make a big difference. When you find a case where the player gets stuck, tweak the settings until you can go through without getting stuck.

    Beyond that, I believe you're on your own. Since you're using Mecanim, you're probably using root motion, so you've told the NavMeshAgent to not control movement. In a script's Update() method or coroutine, keep track of how far the player has moved in, say, the last 2.0 seconds. If the player hasn't moved farther than some minimum distance, change plans. You could give up and switch to an in-place idle state. Or you could try backtracking -- set the destination temporarily to a point 5 units in the opposite direction. This can sometimes get you unstuck, but you run the risk of pacing back and forth between the sticking-point and 5 units behind it. You could always try backtracking just once, then giving up and going to the idle state.
     
    theANMATOR2b likes this.
  8. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,697
    Awesome. Thanks.
    What an impressive tool.
    Thanks Unity!!!