Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
1
Question by DouglasPfeifer · Oct 22, 2015 at 11:57 AM · camerarotationrelative rotationquaternion.slerp

How to rotate an object around itself relative to the camera using Slerp?

I'm making a game and in this game I need to rotate a planted using the mouse left button, so I used this code:

public int speed;
public int lerpSpeed;
private float rotationX;
private float rotationY;
private Quaternion fromRotation;
private Quaternion toRotation;

void Update () {
rotationX -= Input.GetAxis ("Mouse X") * speed;
rotationY -= Input.GetAxis ("Mouse Y") * speed;
toRotation = Quaternion.Euler (rotationY, rotationX, 0);
transform.rotation = Quaternion.Slerp (transform.rotation, toRotation, Time.deltaTime * lerpSpeed);
}
The Slerp works great, however if I hold the mouse left button and move it to the left, making the planet spin 180 degrees around itself and then hold again the mouse left button and move it up, the planet will spin downward instead of spin upward.
I need help on how to solve this, because I have already searched a lot and nothing seems to work.

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 rutter · Oct 22, 2015 at 01:31 PM

It may help to think of rotations in angle-axis format -- any time you rotate an object, you're spinning it some amount around some axis.

In Unity, an Euler angle rotates z degrees around the z-axis, x degrees around the x-axis, and y degrees around the y-axis, in that order. These rotations effect each other -- once you've rotated x degrees around the x-axis, for example, you've pitched forward and changed the "up" axis that you'll rotate around next.

You seem to be expecting rotation in a different order: y-axis first, then x-axis. You can't do that in one call, but you can with two:

 toRotation = Quaternion.identity;
 toRotation *= Quaternion.Euler(0, rotationX, 0);
 toRotation *= Quaternion.Euler(rotationY, 0, 0);

Now, that should solve your immediate problem.

That's world-relative rotation, though. What if the camera isn't aligned to the world? In that case, you can do basically the same thing: rotate around some axis, then rotate around some other axis... the only difference is that the two axes are camera-aligned, instead of world-aligned.

Suppose you have the camera's transform:

 Transform camTransform = Camera.main.transform;

You can build a new target rotation by spinning around the camera's local axes:

 Quaternion targetRot = transform.rotation;
 targetRot *= Quaternion.AngleAxis(rotationX, camTransform.up);
 targetRot *= Quaternion.AngleAxis(rotationY, camTransform.right);

And then rotate towards that:

 float maxDegreesPerSecond = 10f;
 transform.rotation = Quaternion.RotateTowards(transform.rotation, targetRot, maxDegreesPerSecond * 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 DouglasPfeifer · Oct 22, 2015 at 07:06 PM 1
Share

Thanks a lot! You helped me understand a little more on how Quartenion works and helped me solve my problem.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Rotating Player relative to Camera Rotation 2 Answers

How to rotate around the centre of screen on the Y axis in othrographic 0 Answers

Forward and back movements with a camera emulating an isometric view 1 Answer

Rotation seems to pass through condition checks 1 Answer

Quaternion slerp loop exits before equality of quaternions 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