Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 Keyakit · May 31, 2021 at 09:07 PM · 2drigidbody2dcontrolstop-downtop down

Avoiding 2D top-down diagonal movement

Hello Everyone,

I'm working on a 2D game and I'm trying to figure how how to prevent diagonal movement. I've succeeded in preventing the character from moving diagonally, but I have a weird issue.

When I'm moving along the x axis and input to move along the y axis, it works as intended and stops moving horizontally and starts moving vertically. However, when I'm moving along the y axis and try to move horizontally, he will just continue moving up. I'm not entirely sure what's wrong.
public class Controls : MonoBehaviour {

     public float speed = 5;
     public Rigidbody2D rb;
     public Vector2 movement;
 
     // Start is called before the first frame update
     void Start()
     {
         
     }
     // Update is called once per frame
     void Update()
     {
         movement.x = Input.GetAxisRaw("Horizontal");
         movement.y = Input.GetAxisRaw("Vertical");
 
         if (movement.y != 0)
         {
             movement.x = 0;
         }
         if (movement.x != 0)
         {
             movement.y = 0;
         }
 
     }
 
     void FixedUpdate()
     {
         rb.MovePosition(rb.position + movement * speed * Time.deltaTime);
     }
 }
Comment
Add comment · Show 1
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 DenisIsDenis · Jun 01, 2021 at 03:04 AM 0
Share

Firstly, you already asked a similar question:

https://answers.unity.com/questions/1839503/2d-rigidbody-top-down-movement-problem.html

The answer will be similar.

Secondly, it is incorrect to use deltaTime in FixedUpdate. Correctly use fixedDeltaTime.

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Artik2442 · Jun 01, 2021 at 11:20 AM

I already answered at that question but I guess I'll rewrite it.

When your script is running, when y axis is used, you set x axis movement to zero. So, when you press a key that will change x axis, nothing happens (just you keep going on Y axis). To prevent this, you have to check what was the last Axis used by your Keys. So if it was W or S it was Y axis, and else, it was X axis. What I recommend is to check if the value of Horizontal or Vertical have changed. So:

 public class Controls : MonoBehaviour
  {
     public Vector2 movement;
  
  void Update()
     {
          float x = Input.GetAxisRaw("Horizontal");
          float y = Input.GetAxisRaw("Vertical");
  
          float lastX; //To check if current x value has changed from last
          float lastY; //To check if current y value has changed from last
  
          string LastKeyUsed = ""; //will know which axis was changed
  
          if(x!=lastX) //if current x value has changed, we set the last used axis to X and set lastX to current x value
          {
              LastKeyUsed = "X";
              lastX = x;
          }
          if(y!=lastY) //if current y value has changed, we set the last used axis to Y and set lastY to current y value
          {
              LastKeyUsed = "Y";
              lastY = y;
          }
 
          if(x != 0 && y== 0) //In case you stop using the last key and have still an other key pressed, you come back to the other key axis
          {
              LastKeyUsed = "X"
          }
          if(x == 0 && y!= 0)
          {
              LastKeyUsed = "Y"
          }
 
  
          if(LastKeyUsed == "X") //Then we check which axis was last used, and we set movement x and y value depending on the result
          {
              movement.x = x;
              movement.y = 0;
          }
          else if(LastKeyUsed == "Y")
          {
              movement.x = 0;
              movement.y = y;
          }
     }
  }

And voilà! Just keep your FixedUpdate as it is. If any help needed, im here. It might not work, but I hope you got the idea.

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
1

Answer by Tsuki92 · Apr 21 at 12:57 PM

  public float moveSpeed = 5f;
     float lastX; //To check if current x value has changed from last
     float lastY; //To check if current y value has changed from last
 
     string LastKeyUsed = ""; //will know which axis was changed
 
     public Rigidbody2D rb;
     public Animator anim;
 
     Vector2 movement;
     
 
     // Update is called once per frame
     void Update()
     {
       
         
         movement.x = Input.GetAxisRaw("Horizontal");
         movement.y = Input.GetAxisRaw("Vertical");
 
 
         
         
         anim.SetFloat("Speed", movement.sqrMagnitude);
     }
 
 
 
     private void FixedUpdate()
     {
         if (movement.x > 0.01f || movement.x < -0.01f)
         {
             movement.y = 0;
             anim.SetFloat("Horizontal", movement.x);
             anim.SetFloat("Vertical", movement.y);
             rb.MovePosition(rb.position + movement * moveSpeed * Time.fixedDeltaTime);
         }
         else if ( movement.y > 0.01f || movement.y < -0.01f)
         {
             movement.x = 0;
             anim.SetFloat("Horizontal", movement.x);
             anim.SetFloat("Vertical", movement.y);
 
             rb.MovePosition(rb.position + movement * moveSpeed * Time.fixedDeltaTime);
         }
     }

Try this code!, just ignore the parts about animation if you are not using a blend tree.

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 Tsuki92 · Apr 22 at 08:16 AM 0
Share

The code i submitted above locks you from going up while you have left key down.

If you want it to swap between them but still no diagonal rotation just move the code to update() instead of FixedUpdate()

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

282 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 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 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 to turn off gravity? 4 Answers

Rigidbody2D drifitng after collision 2 Answers

How to close an open door? 1 Answer

2d controll rigidboy.velocity.y 1 Answer

Blocking object from going through colliders (no rigidbody) [2D] 1 Answer


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