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 /
  • Help Room /
avatar image
0
Question by hopetheorc · Apr 25, 2018 at 12:13 AM · anglesmoothdamping

SmoothDampAngle problem when the target angle is 90 - 270 degree

Hi guys, I want to use Mathf.SmoothDampAngle to rotate an object which have to follow the given angle. I works well when the given degree is from 0 to 90 and from 180 to 360, but when it turns from 91 to 179 the target object's rotation is totally messes out.

this is my code:

 void Update () {
         var damp = Mathf.SmoothDampAngle(transform.eulerAngles.x, /*Normalise(*/gc.getTravelledDeg()/*, 90, -90)*/ + 10.0f, ref smoothVelocity, 0.5f);
         transform.rotation = Quaternion.Euler(damp, 0, 0);
     }
 
     //Normalizes any number to an arbitrary range 
     //by assuming the range wraps around when going below min or above max 
     float Normalise( float value, float start, float end ) 
     {
         float width = end - start;   // 
         float offsetValue = value - start;   // value relative to 0
 
         return ( offsetValue - ( Mathf.Floor(offsetValue / width ) * width ) ) + start ;
         // + start to reset back to start of original range
     }

I have tried to normalise the angle. I only got a partial result when I used the normalisation from 90 to -90, but it is not what I want. I really don't know what is the problem with my code. Please, help me...

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 hopetheorc · Apr 24, 2018 at 11:27 PM 0
Share

I have tried now to use the simple ascending angle with the same result:

 private float angle = 0;
     void Update () {
         var damp = $$anonymous$$athf.SmoothDampAngle(transform.eulerAngles.x, angle++ + 10.0f, ref smoothVelocity, 0.5f);
         transform.rotation = Quaternion.Euler(damp, 0, 0);
     }

2 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by Bunny83 · Apr 25, 2018 at 12:24 AM

Your problem is that you do a read-modify-write operation on eulerangles which doesn't work as the rotation is internally stored as Quaterion. When you read the current eulerAngles Unity has to convert the Quaterion to euler angles. This conversion is not unique as the same rotation can be represented with different euler angles combinations.


You may want to use your own float variable to store the current rotation without reading it back from eulerAngles.


As you might know euler angles suffer from gimbal lock at x==90° or 270° while quaterions do not have this problem. Gimbal lock generally makes any kind of interpolation between two rotations difficult. That's why we actually use Quaternions.

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 hopetheorc · Apr 25, 2018 at 11:46 AM 0
Share

Thanks for the quick answer :) Can you give me an example please? This my improved script:

         target.transform.rotation = Quaternion.LookRotation(player.transform.position);
 
         var damp = $$anonymous$$athf.SmoothDampAngle(transform.eulerAngles.x, target.eulerAngles.x, ref smoothVelocity, 0.5f);
 
         Debug.Log(target.eulerAngles.x);
 
         Vector3 position = target.position;
         position += Quaternion.Euler(damp, 0, 0) * new Vector3(0, 0, -10);
         transform.position = position;
         transform.LookAt(target);

It is better now, but when the target reaches the gimbal locks it turns backwards...

avatar image
0

Answer by hopetheorc · Apr 25, 2018 at 01:39 PM

Figured it out:

 void FixedUpdate () {
         Quaternion desiredRotation = Quaternion.Euler(gc.getTravelledDeg() + 45, 0, 0);
         Quaternion smoothedRotation = Quaternion.Slerp(transform.rotation, desiredRotation, smoothVelocity);
 
         transform.rotation = smoothedRotation;
     }

It works! :)

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

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

137 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

Related Questions

How to smooth/damp a movement? 0 Answers

Rotate smoothly on sphere 2 Answers

Lerp to Gaze Location Smoothing Motion Issues (Cardboard) 0 Answers

Custom touch control stops abruptly! 0 Answers

Moving to certain direction 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