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 JamesJLinden · Sep 18, 2014 at 11:18 AM · quaterniontopdown

Quaternion.LookRotation Returning Undesired Rotation

I have a top-down 2D game (constrained to xy-plane), and I'm trying to use Quaternion.LookRotation to rotate the player based on the position of the mouse.

Here is a screen recording of the problem: video.

I suspect the transformation is giving me undesired results since my up and look vectors for the camera are different than default.

 // This code is in the FixedUpdate() of the PlayerBehavior component 
 // attached to the player object.
 Vector3 mousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
 Vector3 lookVector = (mousePosition - transform.position);
 lookVector.z = 0.0f;
 transform.rotation = Quaternion.LookRotation(lookVector, new Vector3(0.0f, 0.0f, -1.0f));
 
 // Since I only want the player to be rotated with respect to the z-axis,
 // the x and y components of the rotation are zeroed out.
 transform.eulerAngles = new Vector3(0.0f, 0.0f, transform.eulerAngles.z);

The resulting transformation rotates my player in unusual, unexpected ways that do not resemble my desired result. The intention is for the mouse to rotate the player similar to games like Hotline Miami.

For reference, my up vector is (0, 0, -1), my camera is at (0, 0, -1), my camera's look vector is (0, 0, 1).

Comment
Add comment · Show 1
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 JamesJLinden · Sep 18, 2014 at 10:15 AM 0
Share

So I found a working solution for my problem, but I'm not sure why it works:

 Vector3 mousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
 
 // Z is aligned to (transform.position - mousePosition)
 // Y is aligned to (0, 0, 1)
 Quaternion rot = Quaternion.LookRotation(transform.position - mousePosition, Vector3.forward);
 
 // Remove the x and y components of the orientation
 rot.x = 0.0f;
 rot.y = 0.0f;
 
 // Fully rotate to the new orientation
 transform.rotation = Quaternion.Slerp(transform.rotation, rot, 1.0f);

1 Reply

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

Answer by robertbu · Sep 18, 2014 at 11:24 AM

Directly manipulating the components of a Quaternion is problematic. The most common solution posted on Unity Answers (and it's been posted many times) is as follows:

  Vector3 screenPos = Camera.main.WorldToScreenPoint(transform.position);
  Vector3 dir = Input.mousePosition - screenPos;
  float angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg;
  transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);

This code assumes that the 'forward' side of your sprite is to the right.

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 JamesJLinden · Sep 18, 2014 at 03:00 PM 0
Share

@robertbu Thanks for the answer robert, my bad if this has been answered several times before, I did make a conscious effort to search for similar posts before posting. Your code works great, but I did have to multiply the angle by -1 in order for it to work. I am trying to step through why that's the case.

avatar image JamesJLinden · Sep 18, 2014 at 03:09 PM 0
Share

Figured out why the angle needed to be negative: the rotation is expected to be counter-clockwise and my up vector is (0, 0, -1) since my game is a top-down 2D game. Using my up vector in the AngleAxis call produces a clockwise rotation if not multiplied by -1.

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

2 People are following this question.

avatar image avatar image

Related Questions

A node in a childnode? 1 Answer

Top-down movement in 2D 1 Answer

Multiply quaternion by vector3... how is it done (mathematically)? 1 Answer

Character looking at opposite direction of mouse 1 Answer

2D top-down movement... 6 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