unity animation problems

Hey everyone, So I’m quite new to unity and this is my first time trying out animation, All I wanted to do was create a simple script so that when “W” is held down it plays a walk animation, This script is applied to my character:

var walking = false;
function Update () {
if(Input.GetButtonDown("W")){
walking = true;
}

if(Input.GetButtonUp("W")){
walking = false;
}

while (walking == true)
{
animation.Play("Walk");
}
}

However it doesn’t seem to be working… anyone got any ideas? Or am I just doing the entire animation thing wrong?

Any help would be much appreciated
Many thanks in advance.

I guess that depends on which animation system you’re using. The code is correct for the Legacy system. But for the new Mecanim system you need to set up a state machine with an idle animation and a walk animation and add a transition between the two. While that may seem like a bit more work it is a clear advantage when you begin to add more animations and more complex logic of how to go from one animation to another.

Also its important which game object holds the script. The animation property will give you the legacy animation object directly associated with the game object that the script is residing on. The new animation system uses a component called an Animator and you have to retrieve this with GetComponent.

If you’re just learning Unity now I would definitely recommend that you download a tutorial for the new mecanim animation system and use that. There is a video at http://video.unity3d.com/ that walks through the new animation system. And you can download the “Mecanim Example Scenes” for free on the asset store.

Good luck

Thanks alot:) going to get watching it now