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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by ness3756 · Apr 28, 2017 at 05:07 AM · diagonal

How to disable diagonal movement?

I am trying to disable diagonal movement in unity 2D but it doesn't work.

In this code, what is problem?

 void Update () {
         if (Input.GetKey (KeyCode.D)) {
             East = true; West = false; South = false; North = false;
             Direction ();
             //xMove = moveSpeed * Time.deltaTime;
             //yMove = 0 * moveSpeed * Time.deltaTime;
             //this.transform.Translate (xMove, yMove, 0);
             //yMove = 0 * moveSpeed * Time.deltaTime;
             //public Vector3 UpWalk=transform.Translate.position(new Vector3(moveSpeed*Time.deltaTime,0.0,0.0));
             animator.Play ("Right_Move");
         } else if (Input.GetKey (KeyCode.A)) {
             East = false; West = true; South = false; North = false;
             Direction ();
             //xMove = -moveSpeed * Time.deltaTime;
             //yMove = 0 * moveSpeed * Time.deltaTime;
             //this.transform.Translate (xMove, yMove, 0);
             //yMove = 0 * moveSpeed * Time.deltaTime;
             //public Vector3 =transform.Translate.position(new Vector3(-moveSpeed*Time.deltaTime,0.0,0.0));
             animator.Play ("Left_Move");
         }
         if (Input.GetKey (KeyCode.W)) {
             East = false; West = false; South = false; North = true;
             Direction ();
             //xMove = 0 * moveSpeed * Time.deltaTime;
             //xMove = 0 * moveSpeed * Time.deltaTime;
             //yMove = moveSpeed * Time.deltaTime;
             //this.transform.Translate (xMove, yMove, 0);
             //transform.Translate.position(new Vector3(0.0,moveSpeed*Time.deltaTime,0.0));
             animator.Play ("Up_Move");
         } else if (Input.GetKey (KeyCode.S)) {
             East = false; West = false; South = true; North = false;
             Direction ();
             //xMove = 0 * moveSpeed * Time.deltaTime;
             //xMove = 0 * moveSpeed * Time.deltaTime;
             //yMove = -moveSpeed * Time.deltaTime;
             //this.transform.Translate (xMove, yMove, 0);
             //transform.Translate.position(new Vector3(0.0,-moveSpeed*Time.deltaTime,0.0));
             animator.Play ("Down_Move");
         }
         /*if (Input.GetKey (KeyCode.A) && Input.GetKey (KeyCode.W) || Input.GetKey (KeyCode.D) && Input.GetKey (KeyCode.W)) {
             animator.Play ("Up_Move");
         } else if (Input.GetKey (KeyCode.A) && Input.GetKey (KeyCode.S) || Input.GetKey (KeyCode.D) && Input.GetKey (KeyCode.S)) {
             animator.Play ("Down_Move");
         }*/
         if (Input.GetKeyUp (KeyCode.D) || Input.GetKeyUp (KeyCode.A) || Input.GetKeyUp (KeyCode.W) || Input.GetKeyUp (KeyCode.S)) {
             xMove = 0 * moveSpeed * Time.deltaTime;
             yMove = 0 * moveSpeed * Time.deltaTime;
             this.transform.Translate (xMove, yMove, 0);
             //transform.Translate.position(new Vector3(0,0,0));
             animator.Play ("Playeridle");
         }
         /*if(Input.GetKey(KeyCode.A)&&Input.GetKey(KeyCode.S)||Input.GetKey(KeyCode.A)&&Input.GetKey(KeyCode.W)||Input.GetKey(KeyCode.D)&&Input.GetKey(KeyCode.S)||
             Input.GetKey(KeyCode.D)&&Input.GetKey(KeyCode.W)){
             //this.transform.Translate (xMove,yMove,0);
             xMove = 0 * moveSpeed * Time.deltaTime;
             yMove = 0 * moveSpeed * Time.deltaTime;
             this.transform.Translate (xMove,yMove,0);
             animator.Play ("PlayerIdle");
         }*/
 
     }
 
     void Direction(){
         if (West == true) {
             xMove = -moveSpeed * Time.deltaTime;
             yMove = 0 * moveSpeed * Time.deltaTime;
             this.transform.Translate (xMove, yMove, 0);
         } else if (East == true) {
             xMove = moveSpeed * Time.deltaTime;
             yMove = 0 * moveSpeed * Time.deltaTime;
             this.transform.Translate (xMove, yMove, 0);
         } else if (South == true) {
             xMove = 0 * moveSpeed * Time.deltaTime;
             yMove = -moveSpeed * Time.deltaTime;
             this.transform.Translate (xMove, yMove, 0);
         } else {
             xMove = 0 * moveSpeed * Time.deltaTime;
             yMove = moveSpeed * Time.deltaTime;
             this.transform.Translate (xMove, yMove, 0);
         }
     }
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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by bobisgod234 · Apr 28, 2017 at 06:11 AM

You are calling Direction() twice in an update.

Your code in update, greatly simplified, looks like this:

          if (Input.GetKey (KeyCode.D)) {
              Direction ();
          } else if (Input.GetKey (KeyCode.A)) {
            Direction ();
          }
 
          if (Input.GetKey (KeyCode.W)) {
              Direction ();
          } else if (Input.GetKey (KeyCode.S)) {
            Direction ();
          }

So if you hold D and W at at the same time, it will call Direction() and move east, then it will call Direction() again and move north. Try changing

              if (Input.GetKey (KeyCode.W)) {

to an else if

             else  if (Input.GetKey (KeyCode.W)) {
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 ness3756 · Apr 28, 2017 at 06:22 AM 0
Share

Thank you Very $$anonymous$$uch!! I can solve it!

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

How to move rigidbody Diagonal with movePosition 3 Answers

How can I draw the diagonals of a screen? 2 Answers

Diagonal Movement 1 Answer

How do I move an object along an ellipse path on diagonal? 2 Answers

Disable diagonal movement in unity 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