- Home /
Play animation from animator controler ONLY when a certain button is pressed/hold down.
I'm confused on scripting my character for playing an animation like "walk" when I hold down the button "w". However I want these animations to come from or be connected to the animator controller I made so I make smooth transitions between animations when I do another action that plays a different animation.
Here's my current animator controller: 
Notes: 1. I only want "BR Idle" to play when I'm not pressing any keys
I only want "BR Walk" to play when I'm only holding down the "w" key
I only want "BR Run" to play when I'm holding down "w" and "left shift" keys
I want to make sure the smooth transitions between animations happen (via forward and back from one state to another)
If you have a script that can do all of these requirements. Feel free to answer bellow.
Answer by rutter · Jul 27, 2014 at 05:47 AM
Sounds like you want to set an animation parameter, probably a bool.
Something like:
animator.SetBool("Idle", true); //or false, when button not pressed
You can check if a particular button is pressed using the Input class.
If you can add more detail to this question than "please write my code", we can probably let it through the moderation queue. As it stands, that manual link answers most or all of what you've asked so far.
I still have this error: "The variable 'stateInfo' is assigned but its value is never used."
Hello there rutter,
I want to do something similar. Here is my code.
pragma strict
var animator : Animator;
function Start () { animator = GetComponent("Animator"); }
function Update () {
if(Input.GetButtonDown("PS4_Triangle")) {
animator.SetBool("Triangle", true);
}
else { animator.SetBool("Triangle", false); } }
The thing with this code is that when I press the button that I want it rapidly enables and disables it so quickly that the animation doesn’t get a chance to play. Can you please explain why it might be doing this? Is there something wrong with my code? I’m using Java Script. Thanks in advance.
Your answer
Follow this Question
Related Questions
How to set animator's controller in script? 10 Answers
Basic: How do you get a script to trigger OnMouseOver for moving child? 0 Answers
How to change the speed of an animation? 1 Answer
Animation Vs Animator 2 Answers
How to get default animation for layer 0 Answers