- Home /
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");
}
}
Just use if :P
$$anonymous$$aybe we can help more if you can give us more info.
Yes, please give us more info. You're being vague, and we can't give you a concrete solution if you don't give us a concrete problem.
I think we need something a little more specific. Tell us exactly what you are trying to do and show us some code of how you are trying to accomplish. That way, we can figure out what where you are experiencing a disconnect between your idea and your code.
Answer by sparkzbarca · Mar 18, 2013 at 08:51 PM
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
I used the code and edited it a little and it worked! Thanks so much!
Answer by Screenhog · Mar 18, 2013 at 10:06 PM
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.
Okay. Wait, but how would I do multiple if statements? Sorry
Answer by hoy_smallfry · Mar 19, 2013 at 12:48 AM
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.
Answer by TimHW · Nov 02, 2013 at 12:54 PM
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");
}
}
Your answer
Follow this Question
Related Questions
if statement for 6 ints help C# 2 Answers
problem with instance 1 Answer
Else and If 2 Answers
How stop a piece of code from running if the conditions are no more met. 1 Answer
Why am i getting this error?! 2 Answers