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 /
This question was closed Apr 23, 2015 at 08:25 PM by martin-rohwedder for the following reason:

The question is answered, right answer was accepted

avatar image
1
Question by martin-rohwedder · Apr 20, 2015 at 03:47 PM · movementplayerrpgdiagonal

Disable diagonal movement in unity 2D

Hi all, I am in the proces of creating a little RPG styled game, and in that proces I have created a player movement script. The script itself is working fine, however, when I am holding down two keys (for instance 'W' and 'D'), or is using a joystick, the player object can move diagonally. How can I avoid that from happening?

The player may only go in one of four directions (up, down, left or right), and may not move diagonally. I am using the Input.GetAxisRaw() method atm.

 using UnityEngine;
 using System.Collections;
 
 public class PlayerMovementScript : MonoBehaviour {
 
     public float movementSpeed = 1.5f;
 
     private Animator animator;
 
     // Use this for initialization
     void Start () {
         animator = GetComponent<Animator>();
     }
     
     // Update is called once per frame
     void Update () {
         float xPos = Input.GetAxisRaw("Horizontal");
         float yPos = Input.GetAxisRaw("Vertical");
 
         bool isWalking = (Mathf.Abs(xPos) + Mathf.Abs(yPos)) > 0;
 
         animator.SetBool("isWalking", isWalking);
         if (isWalking) {
             animator.SetFloat("x", xPos);
             animator.SetFloat("y", yPos);
         }
 
         transform.position += new Vector3(xPos, yPos, 0).normalized * Time.deltaTime * movementSpeed;
     }
 }
 
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

  • Sort: 
avatar image
1
Best Answer

Answer by lordlycastle · Apr 20, 2015 at 04:12 PM

It depends on how you want to respond when two keys are pressed? Do you wanna move to last direction pressed, or keep going in the direction you were. You simply need to do check and only assign either x or y values to the input. The following will do the latter of the two scenarios.

 bool mvHoz;
 bool mvVer;
 
 float xPos = 0, yPos = 0;
 if (Input.GetAxis(“Vertical”) != 0 && Input.GetAxis(“Horizontal”) != 0){
     if (mvHoz){
         yPos = Input.GetAxis(“Vertical”);
     }else if  (mvVer){
         xPos = Input.GetAxis(“Horizontal”);
     }
 }else {
     mvHoz = Input.GetAxis(“Horizontal”) != 0;
     xPos = Input.GetAxis(“Horizontal”);
     mvVer = Input.GetAxis(“Vertical”) != 0;
     yPos = Input.GetAxis(“Vertical”);
 }
 
 transform.position += new Vector3(xPos, yPos, 0).normalized * spd * Time.deltaTime;
Comment
Add comment · Show 2 · 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 martin-rohwedder · Apr 20, 2015 at 04:51 PM 0
Share

I couln't get your example to work, unless I changed the GetAxis() method to be GetAxisRaw(), so it's good now, thank you!

avatar image Serch · Oct 02, 2015 at 11:04 PM 0
Share

Could you please explain what would be your method for moving to last direction pressed ?

Follow this Question

Answers Answers and Comments

20 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

Related Questions

Cannot dash in left-down, left-up and right-down directions? 2 Answers

Character Not Moving RPG Game 0 Answers

Circular movement of the player 0 Answers

Help!How to validate the client position in headless unity? 0 Answers

Player movement for iOS? 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