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 /
This question was closed Jul 18, 2019 at 03:03 PM by niklas94 for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by niklas94 · Jul 17, 2019 at 08:33 AM · rotationquaternionupdaterotate objectrotatetowards

Quaternion.RotateTowards() immediately snaps to target rotation

Hi, in the following code I want to roate an object with Quaternion.RotateTowards() inside the Update-Function. Unfortunately the Rotation doesn't take place over a period of time but immediately snaps to the target position(goalRot). the target position is a public Quaternion Variable I set in the Inspector. For the movementSpeed I allready applied values between 0.1f and 10f, makes no different.

     if (rotateToWeight)
     {
         Debug.Log("rotation before: " + transform.rotation);

         // The step size is equal to speed times frame time.
         float step2 = movementSpeed * Time.deltaTime;

         transform.rotation = Quaternion.RotateTowards(transform.rotation, goalRot, step2 );
         Debug.Log("rotation: " + transform.rotation);
         Debug.Log("goalRot: " + transform.rotation);
         Debug.Log("Step: " + step2);

         // Check if Rotation finished
         if (gameObject.transform.rotation == goalRot)
         {
             // Stop Rotating
             rotateToWeight = false;

             Debug.Log("Finished ");
         }
     }  

The console prints the following:

rotation before: (-0.5, -0.5, -0.4, 0.6)
rotation: (-1.0, 0.0, 0.0, 0.0)
goalRot: (-1.0, 0.0, 0.0, 0.0)
Step: 0,09783585
Finished

So, obviousliy the target rotation is allready reached after the first call of the function. No idea why. I would be glad if someone could help

Comment
Add comment · Show 3
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 troien · Jul 17, 2019 at 09:00 AM 2
Share

Before jumping to conclusions here, you might want to actually log the goalRot ins$$anonymous$$d of the current rotation like so...

 Debug.Log("goalRot: " + goalRot);

Also, unless you can read quaternions, it is usually easier to log the eulerangles of these rotations like so:

 Debug.Log("goalRot: " + goalRot.eulerAngles);
avatar image Bunny83 troien · Jul 17, 2019 at 09:40 AM 1
Share

Right. $$anonymous$$y guess is that the goalRot is not a valid normalized quaternion (probably tried setting eulerangles directly to the components of the quaternion). This would result in the dot product between the two quaternions to be way larger than 1. This means the quaternions would be considered equal. That means the angle between them is 0 and RotateTowards would simply return "to".

See the reference for

  • RotateTowards

  • Angle

  • IsEqualUsingDot

If the quaternion is not a unit quaternion, using eulerAngle on it wouldn't give you the right result. So logging the actual quaternion would make more sense when debugging. Though logging both would make most sense.


Like always for Quaternion related questions the reference to the Numberphile video on quaternions as well as the 3b1b video. Even when you're not understanding how a Quaternion works, it might help to actually recognise and distinguish them better from euler angles or other things.

avatar image niklas94 troien · Jul 17, 2019 at 11:42 AM 0
Share

You're both right, I tried to set Eulerangles to the Quaternion components and the angle was 0. $$anonymous$$anaged to solve it, thank you!

1 Reply

  • Sort: 
avatar image
1

Answer by Ossi101 · Jul 17, 2019 at 03:06 PM

Do you have this code inside a loop in Update() ? I tried to replicate your situation and this was my result:

 using UnityEngine;
 
 public class RotateToQuaternionValue : MonoBehaviour
 {
     public bool rotateToWeight = false;
     public Quaternion goalRotation = new Quaternion(-1.0f, 0.0f, 0.0f, 0.0f);
     public float speed = 50.0f;
 
     private void Update()
     {
         if (rotateToWeight)
         {
             float step = speed * Time.deltaTime;
 
             transform.rotation = Quaternion.RotateTowards(transform.rotation, goalRotation, step);
 
             if (transform.rotation == goalRotation)
             {
                 rotateToWeight = false;
             }
         }
     }
 }

This is placed on the object you want to rotate. And in the inspector this is what it looks like:alt text

Setting the bool starts the rotation and it turns off when it has reached the goalRotation value. alt text

I hope this helps!


rotatequaternioninspector.png (8.9 kB)
rotatequaternion.gif (221.8 kB)
Comment
Add comment · 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

Follow this Question

Answers Answers and Comments

148 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 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 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 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 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 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 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 avatar image

Related Questions

How to constantly rotate an object to 0f by X-axis? 2 Answers

Rotate object in the direction where it is going 1 Answer

Change rotation of an object based on an HTC Vive Controller 1 Answer

Best rotation function to use 1 Answer

RotateTowards won't rotate in X axis 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