Slow down and play walk animation on left shift?

Sorry for this silly queston but I’m not good at coding at all. I have a very simple 3rd Person controller script, many animations, but so far I’ve only integrated a ‘run’ and ‘idle.’ I know almost nothing about Javascript and I don’t plan to know more soon especially because my current project requires very little scripting. I know how to add independant animations like ‘bark’ or ‘sit’ on keypress, but I’d love to know how to reduce to 4 speed on my main controller script (below) and crossfade to the walk animation when the left shift is held. This is my script:

function Update () {
    if (Mathf.Abs(Input.GetAxis("Vertical")) > 0.1)
        animation.CrossFade("Run");
    else
        animation.CrossFade("Idle2");
}

I’d also like to be able to play random Idle animations at different times, but I know this is another question…

Thanks!

if (Mathf.Abs(Input.GetAxis(“Vertical”)) > 0.1)
{
// decide between run and walk here
if (Input.GetKey(KeyCode.LeftShift) == true)
{
// we’re walking
MyCharacterSpeedVariable = 4;
animation.CrossFade(“Walk”);
}
else
{
// we’re running
MyCharacterSpeedVariable = 10;
animation.CrossFade(“Run”);
}
}
else
{
animation.CrossFade(“Idle2”);
}

Set the speed variable and values to whatever is appropriate for your project.

The Mecanim tutorial from Unity is perfect for this sort of things. Here it is.

It requires no scripting at all :slight_smile:

I am not sure why you are going for Mathf.Abs(), but I’m still going to use it because it is your code.

function Update() {
   if( Mathf.Abs( Input.GetAxis("Vertical") ) > 0.1 ) {
      
      //Input.GetKey() returns true while the key is being hold down
      if( Input.GetKey( KeyCode.LeftShift ) ) {
         animation.CrossFade("Walk");
      }
      else {
         animation.CrossFade("Run");
      }
   }
   else {
      animation.CrossFade("Idle2");
   }
}

As a bonus, I will propose a solution for your random idle animation

var idleAnimation : string[] = { "idle1", "idle2" .... };    
animation.CrossFade( idleAnimation[ Random.Range(0, idleAnimation.Length) ] );

But you will have to figure out how to time the CrossFade() of the idle animations so that you will get a smoother transition between different idle animations. You can use CrossFadeQueue(), but you will have to figure out how to code that part yourself.