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 Crashade · May 12, 2014 at 04:26 PM · 2djavascriptmovementinputinvert

My controls are inverted when facing sideways.

I just started making a basic 2d top down action game. But already when implementing the basic controls something went wrong. When my character is facing sideways (my move and look keys are seperate) all my movement keys get inverted. Can anyone tell me why this is. My code:

 var speed = 2.0;
 
 function Update () {
     if(Input.GetKey("w")) {
         transform.Translate(transform.up * speed * Time.deltaTime);
         animationController.walker.SetBool("walking", true);
     }
     else if(Input.GetKey("up")) {
         transform.rotation.eulerAngles.z = 0;
     }
     
     
     else if(Input.GetKey("a")) {
         transform.Translate(-transform.right * speed * Time.deltaTime);
         animationController.walker.SetBool("walking", true);
     }
     else if(Input.GetKey("left")) {
         transform.rotation.eulerAngles.z = 90;
     }
     
     
     else if(Input.GetKey("d")) {
         transform.Translate(transform.right * speed * Time.deltaTime);
         animationController.walker.SetBool("walking", true);
     }
     else if(Input.GetKey("right")) {
         transform.rotation.eulerAngles.z = 270;
     }
     
     
     else if(Input.GetKey("s")) {
         transform.Translate(-transform.up * speed * Time.deltaTime);
         animationController.walker.SetBool("walking", true);
     }
     else if(Input.GetKey("down")) {
         transform.rotation.eulerAngles.z = 180;
     }
     else {
         animationController.walker.SetBool("walking", false);
     }
 }
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
Best Answer

Answer by Pyrian · May 12, 2014 at 04:57 PM

Maybe your "transform.up" and "transform.right" should be "Vector3.up" and "Vector3.right" respectively?

Comment
Add comment · Show 5 · 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 Crashade · May 12, 2014 at 05:07 PM 0
Share

Hey, thanks for the answer. I did that and my controls are no longer inverted, but now the direction in which my character move is based on the direction it is face (e.g. when you are looking left and pressing "w" the character walks to the left), which is not what I want.

avatar image Pyrian · May 12, 2014 at 05:22 PM 0
Share

You can make your Translates in world rotation ins$$anonymous$$d of local rotation by adding ", Space.World" in their parameters:

 transform.Translate(Vector3.up * speed * Time.deltaTime, Space.World);
avatar image Crashade · May 12, 2014 at 05:42 PM 0
Share

Yeah that fixed it. Thanks a lot! $$anonymous$$aybe you could help me with another little problem? Currently when I am moving, I can't change the direction I'm looking. So I have to stop, to turn around. Do you know of any way of fixing this?

avatar image Pyrian · May 12, 2014 at 05:55 PM 0
Share

That's because your single if-else-if structure only allows one input at a time. (I thought it was odd, but refrained from commenting on it.) You'll probably want to separate it into two if-else-if's, the first with all the directions, and the second with the movement.

Glad I could help! :)

avatar image Crashade · May 12, 2014 at 06:12 PM 0
Share

Alright! Again thank you so much for all the help :)

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

21 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

Related Questions

how to move an object in 2D sense its position? 0 Answers

Why does my enemies' movement change whenever my player moves toward and away from the enemies' current position? 1 Answer

How to create random movement in 2D 2 Answers

Translating Object Continually 1 Answer

2D Top-Down Movement: Transform.up and right don't change 2 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