Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 TurtleDuck · Dec 16, 2013 at 07:12 AM · 2drotationmovementcontrolstopdown

Top down 2d movement without being affected by rotation

Hey guys,

Currently i am working with Unity 4.13 using the new 2d features to make a top down 2d adventure game in the style of games like the 2d Zelda's. Currently i have it so the player rotates to face the mouse but is locked to the 90 degree angles (up, down, left, right) and no others. My issue is when the rotation is changed (it's the z axis that i am changing) the movement no longer stays the same. For example when the rotation is 0 (the default, facing up) the player moves in the correct direction, up is up, left is left and so on. But when the rotation changes the player directions get messed up and up becomes down and left becomes right, or up becomes left.

I have tried changing the transform.forward but i have gotten some really weird rotation bugs from that so i am pretty sure i did it wrong if that is the correct way to fix this.

Here is what i have for my movement and my rotation:

 transform.position += transform.right * Input.GetAxis ("Horizontal") * speed * Time.deltaTime;
 transform.position += transform.up * Input.GetAxis("Vertical") * speed * Time.deltaTime;

 mousePos = new Vector3(Input.mousePosition.x, Input.mousePosition.y, Camera.main.transform.position.y-transform.position.y);
 mousePos = Camera.main.ScreenToWorldPoint(mousePos);
 playerRotPos = mousePos - transform.position;
 angle = -Mathf.Atan2 (playerRotPos.y, playerRotPos.x) *Mathf.Rad2Deg + 0.0f;
 angle = Mathf.Round(angle/90.0f) * 90.0f;
 transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);

Any help would be great as i am stuck on this problem. Thanks in advance. :)

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

2 Replies

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

Answer by robertbu · Dec 16, 2013 at 07:15 AM

transform.right and transform.up are the local 'up' and 'right' relative to the game object. It seems to me you want world coordinates. Replace the first two lines:

 transform.position += Vector3.right * Input.GetAxis ("Horizontal") * speed * Time.deltaTime;
 transform.position += Vector3.up * Input.GetAxis("Vertical") * speed * Time.deltaTime;
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 TurtleDuck · Dec 16, 2013 at 07:17 AM 0
Share

Thank you so much for that. :)

avatar image
0

Answer by Reals · Feb 19, 2017 at 10:34 PM

Note: The "Best Answer" in this topic takes away all physics properties from the player game objects and won't work properly with colliders.

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 vanshTandon · Aug 31, 2021 at 06:28 PM 0
Share

How can i make without effecting the colliders and rotation ??

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

18 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

Related Questions

How Can I Rotate My 2D Top-Down Character Rotation With Mobile Joystick? 0 Answers

Top-Down Movement in Unity 1 Answer

How do I make a 2D object face the direction it is moving in top down space shooter 1 Answer

How do I detect when my player is moving in unity 2d? 1 Answer

Rotation while movement problem 3 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