Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 AWDXZA · Jun 11, 2019 at 07:00 AM · 2d gamemovement scriptplayer movementif-statementsorder

How can I deal with multiple if-statements?

Hello, I'm a new inexperience programmer and I'm trying to make a 2D game.

I'm currently having problems with a player controller script, for 2D player movement. In the script, what I want to do is changing the player's direction by pressing "Horizontal" or "Vertical" Inputs while pressing the other inputs.

Ex: When I press "Horizontal" input then move horizontally, when I press "Vertical" input while pressing the "Horizontal" input then change direction and move vertically.

But somehow my script isn't working when I press the "Vertical" input while I'm pressing the "Horizontal" input. (Oddly, when I press the "Horizontal" input while I'm pressing the "Vertical" input, it works fine)

So here is my script:

 > public class PlayerController : MonoBehaviour                                                                                                {
 public Rigidbody2D theRB;
 public float theSpeed;
 public static PlayerController instance;

 void FixedUpdate()
 {
     theRB.velocity = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical")) * theSpeed;

     if (Input.GetAxisRaw("Horizontal") == 1 || Input.GetAxisRaw("Horizontal") == -1)
     {
         if (Input.GetAxisRaw("Vertical") == 1 || Input.GetAxisRaw("Vertical") == -1)
         {
             theRB.velocity = new Vector2(0, Input.GetAxisRaw("Vertical")) * theSpeed;
         }
         else if (Input.GetAxisRaw("Horizontal") == 1 || Input.GetAxisRaw("Horizontal") == -1)
         {
             theRB.velocity = new Vector2(Input.GetAxisRaw("Horizontal"), 0) * theSpeed;
         }
     }
     if (Input.GetAxisRaw("Vertical") == 1 || Input.GetAxisRaw("Vertical") == -1)
     {
         if (Input.GetAxisRaw("Horizontal") == 1 || Input.GetAxisRaw("Horizontal") == -1)
         {
             theRB.velocity = new Vector2(Input.GetAxisRaw("Horizontal"), 0) * theSpeed;

         }
         else if (Input.GetAxisRaw("Vertical") == 1 || Input.GetAxisRaw("Vertical") == -1)
         {
             theRB.velocity = new Vector2(0, Input.GetAxisRaw("Vertical")) * theSpeed;
         }
     }
 }

Thank you for your help!

Comment
Add comment
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

2 Replies

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

Answer by AWDXZA · Jun 26, 2019 at 05:04 AM

Okay, I solved the problem!

To answer my own question, the problem was that the if statements with Input.GetAxisRaw didn't recognized the order of the vertical and horizontal inputs. So what I mean is that to my code,

 if (Input.GetAxisRaw("Horizontal") == 1 || Input.GetAxisRaw("Horizontal") == -1)
         {
             if (Input.GetAxisRaw("Vertical") == 1 || Input.GetAxisRaw("Vertical") == -1)

and

 if (Input.GetAxisRaw("Vertical") == 1 || Input.GetAxisRaw("Vertical") == -1)
         {
             if (Input.GetAxisRaw("Horizontal") == 1 || Input.GetAxisRaw("Horizontal") == -1)

basically are same, since the code don't recognize the order of the two inputs.

So in order to set the order of these two inputs, I used Time.fixedDeltaTime and made the differences of the two inputs. So the final code looks like this:

 public Rigidbody2D theRB;
 public float theSpeed;
 
 private float horizontalTime=0;
 private float verticalTime=0;
 
     void Update()
     {
         TimerForMove();
 
         theRB.velocity = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical")) * theSpeed;
 
         if (Input.GetAxisRaw("Horizontal") == 1 || Input.GetAxisRaw("Horizontal") == -1)
         {
             if (Input.GetAxisRaw("Vertical") == 1 || Input.GetAxisRaw("Vertical") == -1)
             {
                 if (horizontalTime > verticalTime)
                 {
                     theRB.velocity = new Vector2(0, Input.GetAxisRaw("Vertical")) * theSpeed;
                 }
                 else theRB.velocity = new Vector2(Input.GetAxisRaw("Horizontal"), 0) * theSpeed;
             }
             //else if (Input.GetAxisRaw("Horizontal") == 1 || Input.GetAxisRaw("Horizontal") == -1)
             {
                 //theRB.velocity = new Vector2(Input.GetAxisRaw("Horizontal"), 0) * theSpeed;
             }
         }
         if (Input.GetAxisRaw("Vertical") == 1 || Input.GetAxisRaw("Vertical") == -1)
         {
             if (Input.GetAxisRaw("Horizontal") == 1 || Input.GetAxisRaw("Horizontal") == -1)
             {
                 if (verticalTime > horizontalTime)
                 {
                     theRB.velocity = new Vector2(Input.GetAxisRaw("Horizontal"), 0) * theSpeed;
                 }
                 else theRB.velocity = new Vector2(0, Input.GetAxisRaw("Vertical")) * theSpeed;
             }
             //else if (Input.GetAxisRaw("Vertical") == 1 || Input.GetAxisRaw("Vertical") == -1)
             {
                 //theRB.velocity = new Vector2(0, Input.GetAxisRaw("Vertical")) * theSpeed;
             }
         }
     }
 
     void TimerForMove()
     {
         if (Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.D))
         {
             horizontalTime += Time.fixedDeltaTime;
         }
         else
         horizontalTime = 0;
 
         if (Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.S))
         {
             verticalTime += Time.fixedDeltaTime;
         }
         else
         verticalTime = 0;
 }

When I tested, the player moved as what I expected and can be used as simple 2D player movement script!

Thank you for your help and if you have better code or suggestion for fixing some of this code, please let me know ;)

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 ShadyProductions · Jun 11, 2019 at 08:32 AM

You don't need all those if statements you already are setting the velocity using this one line:

 theRB.velocity = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical")) * theSpeed;

But you may want to get the input from the update method instead and pass it to the fixed update method.

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 AWDXZA · Jun 11, 2019 at 05:08 PM 0
Share

Well, what I want to do is changing the player's direction by pressing the other input. Also, I forgot mention but my another purpose for all those if statements are for limiting the player's movement by 4 directions. So I think I'll need all those if statements. But yes, I think I may need to get the input from the update method ins$$anonymous$$d of the fixed update method. Thank you for the help!

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

133 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 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 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 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 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 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 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 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 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

How can I limit my 2D player movement along the Y-axis? 1 Answer

My animation is overriding my character movement? 2 Answers

Snake Movement Problem 0 Answers

Move 2D Sprite Horizontally With Mouse 1 Answer

Player Dash doesn't work properly 0 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