Play animation via script

Hi All,

I have several animations for my character.

My simple script looks like that :

if (Input.GetAxis("Vertical"))
    animation.Play("walk");
else
   animation.Play("static");

I’ve set warpMode to loop.

Now when I hold down the w key my character starts to walk as I want but when I only tap on the w key the walking animation only play only few frame of the total walking animation. Why? I expect even with only tapping on the w key to play the all walking animation since I call the animation.Play method

Thank u in advance

There are a lot of ways to script something like this, and it’s normally a very involved process that takes careful planning based on what the gameplay needs are of your game, but if you just need to get it working quickly right now, try this:

function Start(){

    //set the animation wrap mode here manually for more
    //control; some animations should be looped, this depends on your game!

    animation["Walk"].wrapMode=WrapMode.Loop;

    animation["Static"].wrapMode=WrapMode.Loop;

    

    //set the animation layers correctly; this depends on the type of
    //game your making- don't work on a character system without
    //    setting your animation layers correctly!!

    //    Also, if you don't explicitly set layers for each animation, Unity
    //    defaults them to layer 0

    animation["Walk"].layer=0;

    animation["Static"].layer=0;

    

}



function Update(){

    if (Mathf.Abs(Input.GetAxis("Vertical"))>0.1)

        animation.CrossFade("Walk");

    else

        animation.CrossFade("Static");

}

There’s a lot more that goes into a character system, a lot more. If you’re just fooling around with Unity, the above code is ok. However, for production, you should careful plan out first how your character will move, animations needed, functionality needed, etc. and develop a good system on paper or a project planning program. Once you have a clear direction, then and only then would I begin coding something in Unity. Hope that clears some things for you, have fun and take care.

P.S. It’s 6am here and I’ve been pulling an all nighter programming with Unity, so if the code I gave you won’t work my apologies- but my brain is fried right now

Thank u for your answer

I probably didn’t make myself clear.

My question is why when even tapping (not holding the key down for couple of seconds) on the vertical key (with regards to the above code) the walking animation is partially playing and not the all frames of the animation are playing. After all the code says animation.CrossFade(“walk”) and only when I hold the button down the whole walking animation is shown

Thanx again

So you want to tap the move button quickly and have the walk animation play even after you are not pressing anymore? You want the character to autowalk or something?

Not following you, sorry.

exactly :slight_smile:
After all when Input.GetAxis returns true the animation must be played (according to the script), so why only tiny part of the animation is played??

The reason the moment you stop moving the walk stops, is because of the the else statement.

The code basically says:
“if we’re moving, we play walk animation, however, if we stop moving, then play the static animation. Remember that when we release the moving forward button, our movement mathematically becomes zero, which is when the script is programmed to play the static animation.”
I think for what you need, you will have to do something else, perhaps add a yield command in the if statement so that there is a small delay before the script kicks in the static animation. A yield statement is easy:

yield WaitForSeconds (3.0);

I put 3.0, but you should see how long is your animation, let’s say it’s exactly 2.4 seconds long, then use that number instead and the script will basically wait 2.4 seconds (or however long your animation is, you decide) and then it will kick in the static animation only after the yield time has passed, thus letting you get the effect you want, if I understand you correctly. Have to be honest, I still am not even sure if this is what you are after…

Thank u very much

You did solve my problem :slight_smile:

Thanx

Score one for the good guys! j/k

Glad to have been able to help :wink: