How to do multiple 'if' statements for multiple different actions

Hi, I was wondering, if I wanted to do two different ‘if’ statements for two different actions, how would I do it?

What I mean is, whenever I use an if statement, and try to add in another, it won’t work. Do you have to add in an extra function, or do you use ‘else if’?

I’m wondering what you would do if you wanted two if statements for two DIFFERENT animations or actions, so I don’t think ‘else if’ or an extra function would do the trick. I can’t seem to add another if statement without something going on…

Here is the code I am trying to do:

#pragma strict

var walkSpeed = 0.2;
var runSpeed = 1;
var flySpeed = 3;

function Start () {

}

function Update () {
 if (Input.GetAxis("Vertical")) {
animation.CrossFade("Walk_");
}
else {
animation.CrossFade("Rest");
}
}
{
 if (Input.GetAxis("Horizontal")) {
animation.CrossFade("Walk_");
}
else {
animation.CrossFade("Rest");
}
}

if(i’m jumping){
do this
}

else if(im standing){
do this
}

else if (i'm poopin){
do this

}
else //anything else
do this 

i’m assuming you want something like that

What about something like this?

function Update () {

var inputAmounts = Vector2(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"));
if (inputAmounts == Vector2(0.0, 0.0)) {
  //action that happens when no arrow keys are being pressed
  animation.CrossFade("Rest");
} else {
  //action that happens when arrow keys have been pressed
  animation.CrossFade("Walk_");
}

}

This should do what you’re looking for, but be aware that this doesn’t scale well. If you want to start adding other animations, or doing other things, you’ll need a more advanced system, or at least more statements within this code.

You may also be better off using Input.GetAxisRaw instead or Input.GetAxis to test for this.

Ah, I understand now.

So, If you are ever unclear about how to use a function or class that Unity provides, take a look at the Unity Script Reference. From there, you can read up on how to properly use Input.GetAxis(), which returns a float somewhere between -1.0 and 1.0.

Save these values like so:

var direction = new Vector2(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical");

When you let go of the keys, x and y will be zeros, and when you press them they should change to either 1 or -1 depending on what direction you press in.

Now, what you want, I assume, is to take the values and figure out how to crossfade between walking and running? If you get the magnitude of the Vector, you can find the “strength” of the fade:

animation.Blend(“Walk_”, direction.magnitude, 0);
animation.Blend(“Rest”, 1 - direction.magnitude, 0);

While while moving forward, “Rest” will have a strength of 0, “Walk_” a strength of 1, and while standing still, “Walk_” will have a strength of zero and “Rest” a strength of 1.

If that’s too abrupt, then instead of directly using direction.magnitude, filter it through another equation to get a smoother blend.

As this has not been pointed out, the code you used as an example had problems:

function Update () {
    if (Input.GetAxis("Vertical")) {
        animation.CrossFade("Walk_");
    }
    else {
        animation.CrossFade("Rest");
    }
} // closed brace which ends the update function.
{ // wild open brace which will never be entered, meaning nothing inside it will be called.
  // I assume this if statement inside here is what you were implying was not working.
    if (Input.GetAxis("Horizontal")) {
        animation.CrossFade("Walk_");
    }
    else {
        animation.CrossFade("Rest");
    }
} // wild close brace.

With the wild braces removed and the second if statement inside the update function, the two if statements will be called without any problem. Although, as you are wanting to do the same animation for both vertical and horizontal axis input, you would be able to do what you want with one if statement:

function Update () {
    if (Input.GetAxis("Vertical") || Input.GetAxis("Horizontal")) {
        animation.CrossFade("Walk_");
    }
    else {
        animation.CrossFade("Rest");
    }
}