Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by gamezdragon · Mar 18, 2013 at 08:26 PM · ifelse

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");
 }
 }
Comment
Add comment · Show 4
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image dorpeleg · Mar 18, 2013 at 08:39 PM 0
Share

Just use if :P

$$anonymous$$aybe we can help more if you can give us more info.

avatar image hoy_smallfry · Mar 18, 2013 at 08:42 PM 0
Share

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.

avatar image gamezdragon · Mar 18, 2013 at 08:51 PM 0
Share

Ok, sorry. I'll edit my post.

avatar image hoy_smallfry · Mar 18, 2013 at 09:16 PM 0
Share

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.

4 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image gamezdragon · Mar 18, 2013 at 08:54 PM 0
Share

Sort of, but I still don't get it. I'm sorry

avatar image gamezdragon · Mar 18, 2013 at 08:55 PM 0
Share

I edited my post. Hopefully it's more clear

avatar image gamezdragon · Mar 19, 2013 at 03:54 PM 0
Share

I used the code and edited it a little and it worked! Thanks so much!

avatar image
0

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image gamezdragon · Mar 18, 2013 at 11:58 PM 0
Share

Okay. Wait, but how would I do multiple if statements? Sorry

avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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");
     }
 }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

15 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges