• 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 /
  • Help Room /
avatar image
0
Question by Crafterguy · Aug 10, 2017 at 03:49 PM · movementmovediagonalincorrect

My player moves diagonally when you press two keys at once

The player in my RPG I was starting to develop moves diagonally when you press two arrow keys at once. I thought it would be easy to stop t$$anonymous$$s movement but, I could not do it. Here is what I tried: public class Move: MonoBehaviour {

 public Transform playerPos;
 public Animator Ani;
 public float moveSpeedX;
 public float moveSpeedY;
 private bool MovingX;
 private bool MovingY;
 private Vector2 lastMove;

 private void Start()
 {
     Ani = GetComponent<Animator>();
     
 }
 void FixedUpdate () {
     MovingX = false;
     MovingY = false;
     moveSpeedX = 8.5f;
     moveSpeedY = 8.5f;
     if (Input.GetAxis ("Horizontal") > 0.4f || Input.GetAxis ("Horizontal") < -0.4f && MovingY == false) {
             lastMove = new Vector2(Input.GetAxis("Horizontal"), 0f);
             MovingX = true;
             playerPos.Translate(new Vector3(Input.GetAxis("Horizontal") * moveSpeedX * Time.deltaTime, 0f, 0f));

     }
     if (Input.GetAxis ("Vertical") > 0.4f || Input.GetAxis ("Vertical") < -0.4f && MovingX == false) {
             lastMove = new Vector2 (0f, Input.GetAxis ("Vertical"));
             MovingY = true;
             playerPos.Translate (new Vector3 (0f, Input.GetAxis ("Vertical") * moveSpeedY * Time.deltaTime, 0f));
         }
     Ani.SetFloat("X", Input.GetAxis("Horizontal"));
     Ani.SetFloat("Y", Input.GetAxis("Vertical"));
     Ani.SetBool("Moving",MovingX == true || MovingY == true ? true:false);
     Ani.SetFloat("LastX", lastMove.x);
     Ani.SetFloat("LastY", lastMove.y);

 }

}

I changed my code so there was two Moving variables. Originally there was only a Moving variable but, I created MovingX and MovingY. I made it so the player can only move left and right when MovingY is false and vice versa. I though t$$anonymous$$s would fix the issue but, I can still move diagonally when I am going up and press right arrow but, when I move down and hold left I continue to move down. Can someone please tell what I have done wrong and I can get the player to continue moving in the direction he originally was when another arrow key is pressed.

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

0 Replies

· Add your reply
  • Sort: 

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

139 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

Related Questions

What is the correct way to move objects relative to each other ? 1 Answer

Diagonal Movement Speed Boost Problem HELP 0 Answers

How to move an object with a starting and ending speed and time 0 Answers

Move player based on UI controls 0 Answers

Stop diagonal movement 2d top down game 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