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 hankphone1 · Sep 17, 2013 at 10:28 PM · 2dcharactercontrollercontrollerdisablediagonal

Disable diagonal movement on 2D character controller

Hey,

I am trying to disable diagonal movement in a really simple 2D character controller. I have been trying all sorts of things with no real luck at all.

Basically, when two movement keys (i.e. "w" & "d") are pressed at the same time I want the controller to ceither ontinue along the axis of whichever button was pressed first or just not move at all. Looking for something similar to 2D grid based movement without it being grid based.

Any help would be appreciated greatly.

Thanks!

 var speed : float = 15;
 
 function Update () {
 
     if(Input.GetButton("Vertical")) {
         transform.Translate(Vector3(0,speed,0) * Time.deltaTime);
     }
 
     if(Input.GetButton("Horizontal")) {
         transform.Translate(Vector3(speed,0,0) * Time.deltaTime);
     }
 
     if(Input.GetButton("-Vertical")) {
         transform.Translate(Vector3(0,-speed,0) * Time.deltaTime);
     }
 
     if(Input.GetButton("-Horizontal")) {
         transform.Translate(Vector3(-speed,0,0) * Time.deltaTime);
     }
 
 }
Comment
Add comment · Show 2
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 robertbu · Sep 17, 2013 at 10:29 PM 0
Share

If you put an 'else' in front of your last three 'if' statements, you will eli$$anonymous$$ate diagonal movement. Note the preference will be in the order of the 'if' statements. To capture "whichever button was pressed first" would be far more complicated.

avatar image Fattie · Sep 18, 2013 at 09:06 AM 0
Share

@robster - what you're saying would work of course (as would many other changes). But don't you agree that what s/he "meant" was to have returns in there.

It's such a common mistake for beginners. Sometimes I think languages should just breakaway after an if !

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Fattie · Sep 18, 2013 at 09:04 AM

What you are doing here is writing

breakaway code.

.

Quite simply, you forgot the "return" inside each clause. That's what you "meant" when you were thinking about it, you just forgot to type the "return" calls. It is an exceptionally common beginner mistake.

(Note that there are huge philosophical issues relating to whether one should write "breakaway code" and for people who do things like "think of algorithms" it is a topic of obsession. Furthermore your whole approach to picking up buttons like that may be done in utterly different and perhaps better ways. But the fact is in the situation at hand you simply "forgot the returns" - that's what you meant in your head, you wanted it to do one of those and that's it - right?)

Hope it helps.

 function Update()
     {
     if(Input.GetButton("Vertical"))
         {
         transform.Translate(Vector3(0,speed,0) * Time.deltaTime);
         return;
         }
 
     if(Input.GetButton("Horizontal"))
         {
         transform.Translate(Vector3(speed,0,0) * Time.deltaTime);
         return;
         }
 
     if(Input.GetButton("-Vertical"))
         {
         transform.Translate(Vector3(0,-speed,0) * Time.deltaTime);
         return;
         }
 
     if(Input.GetButton("-Horizontal"))
         {
         transform.Translate(Vector3(-speed,0,0) * Time.deltaTime);
         return;
         }
 
     }
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 hankphone1 · Sep 18, 2013 at 02:12 PM 0
Share

Thanks Fattie, that is indeed what I was thinking. You were a great help!

I'm sure once I learn more I'll try some different ways to create controllers.

Thanks!

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

16 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

Related Questions

Modify movement script for continuous movement 1 Answer

Passing through walls. 2d toolkit 1 Answer

What is Happening here? please help. 0 Answers

(2d - top down) Characters drifting upon movement,Characters drifting upon movement (2d) 1 Answer

How to use multiple bone rigged sprites for 2d character animations 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