- Home /
If this Key is pressed do this animation
this problem is currently stopping me from advancing in unity3d. I have seen the mecanim tutorial and I read the BotController script(it was in C# and i use java so i don't understand it). What I want to do is make it so that when e.g. The w key is pressed, it makes a transition to the walk animation and if i let go it goes back to idle
Answer by ivan2532 · Aug 08, 2013 at 11:38 AM
Hello, tinashee, you can use animation.CrossFade, here is the code:
JS:
var player : GameObject;
function Update()
{
if(Input.GetKeyDown(KeyCode.W))
{
player.animation.CrossFade("Walk");
}
if(!Input.anyKey)
{
player.animation.CrossFade("Idle");
}
}
C#
public GameObject player;
void Update()
{
if(Input.GetKeyDown(KeyCode.W))
{
player.animation.CrossFade("Walk");
}
if(!Input.anyKey)
{
player.animation.CrossFade("Idle");
}
}
does the "Walk" have to be the name of the animation of the walking man
I get the error- the animation state Idle could not be played because it couldn't be found. I created an animation component and added the idle animation to it though...
Your answer
![](https://koobas.hobune.stream/wayback/20220613111750im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
player animation dont work 0 Answers
AirPlane Sway Effect 1 Answer
Light bulb moving HELP 1 Answer
Animation Problem 2 Answers