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 /
avatar image
0
Question by rewclaus · May 02, 2014 at 06:30 AM · 2dmovementdiagonalpriority

Prioritize Movement in 2D

Hello. I am quite new to Unity and C#. I am trying to create a movement script that allows a player to move only in the cardinal directions (U, D, L, R). The code I currently have allows me to move in those four directions without moving in the diagonals. However, there are some issues.

When I hold the up or down key, and then press the left or right key the movement stays either up or down. This differs when I am holding left or right, and press and hold up or down, it prioritizes the up or down movement. Is there a way to have no priority so that when I am holding down one key, the second I push another key (regardless if I am still holding the original key or not) that priority passes to second key? Is the priority defined by he location of the if statements?

Thanks for any help!

 using UnityEngine;
 using System.Collections;
 
 public class PlayerMovement : MonoBehaviour {
 
 
     public Animator anim;
     public float speed = 5.0f;
 
     void Start ()
     {
         anim = GetComponent<Animator>();
     }
 
 
     // Update is called once per frame
     void Update ()
     {
         Movement ();
     }
 
     void Movement()
     {
         anim.SetFloat ("vSpeed", Input.GetAxisRaw ("Vertical"));
         anim.SetFloat ("hSpeed", Input.GetAxisRaw ("Horizontal"));
 
         if(Input.GetAxisRaw ("Vertical")>0)
         {
             transform.Translate(Vector2.up*speed*Time.deltaTime);
             anim.SetFloat ("hSpeed",0);
             return;
         }
         if(Input.GetAxisRaw ("Vertical")<0)
         {
             transform.Translate(-Vector2.up*speed*Time.deltaTime);
             anim.SetFloat ("hSpeed",0);
             return;
         }
         if(Input.GetAxisRaw ("Horizontal")>0)
         {
             transform.Translate(Vector2.right*speed*Time.deltaTime);
             anim.SetFloat ("vSpeed",0);
             return;
         }
         if(Input.GetAxisRaw ("Horizontal")<0)
         {
             transform.Translate(-Vector2.right*speed*Time.deltaTime);
                     anim.SetFloat ("vSpeed",0);
             return;
         }
     }
 }
 
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

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by darthtelle · May 02, 2014 at 07:17 AM

I suspect your code is hitting the returns in either your up or down if statements so it's never going to reach your left or right checks. Try removing the returns and putting the x and y in an if-else statement instead.

        if(Input.GetAxisRaw ("Vertical")>0)
        {
          transform.Translate(Vector2.up*speed*Time.deltaTime);
          anim.SetFloat ("hSpeed",0);
        }
        else if(Input.GetAxisRaw ("Vertical")<0)
        {
          transform.Translate(-Vector2.up*speed*Time.deltaTime);
          anim.SetFloat ("hSpeed",0);
        }

        if(Input.GetAxisRaw ("Horizontal")>0)
        {
          transform.Translate(Vector2.right*speed*Time.deltaTime);
          anim.SetFloat ("vSpeed",0);
        }
        else if(Input.GetAxisRaw ("Horizontal")<0)
        {
          transform.Translate(-Vector2.right*speed*Time.deltaTime);
          anim.SetFloat ("vSpeed",0);
        }
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 rewclaus · May 03, 2014 at 12:18 AM 0
Share

This recreates the diagonal movement issue.

avatar image
0

Answer by Maerig · May 02, 2014 at 07:57 AM

If you do want to only have the last pressed key active at a time, this would be a solution

 private enum EDirection {
         NONE       = 0,
         HORIZONTAL = 1,
         VERTICAL   = 2
 }
 
 private EDirection m_lastDirection = EDirection.NONE;
 
 private bool m_isMovingVertically   = false;
 private bool m_isMovingHorizontally = false;
 
 void Update () {
     float vertical   = Input.GetAxisRaw ("Vertical");
     float horizontal = Input.GetAxisRaw ("Horizontal");
     if(vertical == 0f) {
         m_isMovingVertically = false;
     }
     if(horizontal == 0f) {
         m_isMovingHorizontally = false;
     }
     if(vertical == 0f && horizontal == 0f) {
         m_lastDirection = EDirection.NONE;
         return;
     }
 
     if(!m_isMovingVertically && vertical != 0f) {
         // The user has just pressed a vertical key
         m_lastDirection = EDirection.VERTICAL;
         anim.SetFloat("hSpeed",0);
         m_isMovingVertically = true;
     }
     else if(!m_isMovingHorizontally && horizontal != 0f) {
         // The user has just pressed an horizontal key
         m_lastDirection = EDirection.HORIZONTAL;
         anim.SetFloat("vSpeed",0);
         m_isMovingHorizontally = true;
     }
 
     Vector3 movement = Vector3.zero;
     switch(m_lastDirection) {
     case EDirection.VERTICAL :
         movement = (vertical * Vector2.up).normalized;
         break;
     case EDirection.HORIZONTAL :
         movement = (horizontal * Vector2.right).normalized;
         break;
     default :
         break;
     }
     transform.Translate(movement * speed * Time.deltaTime);
 }

It would probably be easier with Input.GetKeyUp, though.

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 rewclaus · May 04, 2014 at 12:11 AM 0
Share

I also had issues with this implementation. When I would hold one direction and switch to another, it would lock in that position, and so when I would try and switch again, it would not switch. That is a pretty poor description, but it would be better to see the issue than to tell you.

I am looking for a Final Fantasy 3/6 movement style for my project. I want to be able to be holding u/d (r/l), and then while still holding u/d (r/l) and I push the r/l (u/d) button that it switches the movement of the character. $$anonymous$$aybe this is just too complicated for me to implement.

avatar image
0

Answer by Jeff-Kesselman · May 04, 2014 at 12:52 AM

If you hold two keys at once on a typical PC keyboard you may get one, the other, or an entirely DIFFERENT key (this is called "ghosting"). This is at the hardware level and there isn't a lot you can do about it.

http://www.microsoft.com/appliedsciences/antighostingexplained.mspx

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

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

23 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

Related Questions

How do I make my balls initial speed make it go in a diagonal direction while still being able to bounce off of walls? 1 Answer

2D Sprite leaves a trail when moving diagonally 1 Answer

How can I make 2D movement less jerky on a controller, with velocity and such? 0 Answers

wasd movement rigidbody no bouncing 0 Answers

Gravity switch 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