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
1
Question by joseques · Aug 21, 2014 at 05:43 PM · rotationvector3lerpeulerangles

Lerping euler angles make entire spin

I have this script to make a lerp to the Euler Angles of a Quaternion. When the second parameter of the Lerp (to) it's positive, works like a charm but when the second parameter it's negative, the Transform makes an entire spin.

             if(yawNeg){
                 relativeRot = -(Vector3.Lerp(
                     rudders[0].localRotation.eulerAngles,//first parameter
                     (maxRot * ruddersVectorRot),//second parameter
                     Time.deltaTime*5));
             } else if(yawPos){
                 relativeRot = Vector3.Lerp(
                     rudders[0].localRotation.eulerAngles,
                     maxRot * ruddersVectorRot,
                     Time.deltaTime*5);
             } else {
                 relativeRot = Vector3.Lerp(
                     rudders[0].localRotation.eulerAngles,
                     Vector3.zero,
                     Time.deltaTime*5);
             }

I've inverted the absolute value of the Lerp so I have my desired negative value. I know that the error resides on the yawNeg part.

What I'm doing wrong?

Here's a screenshot, I've taken it with Lightshot due to Unity Answers size limitation on images. http://prntscr.com/4f1y0t

EDIT: I've fotgot to say if I invert something, all messes up the rotation. Even if I invert only the second parameter or the whole Lerp. If the relativeRot Vector3 is negative, the rotation makes an entire spin.

Comment
Add comment · Show 5
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 Huacanacha · Aug 21, 2014 at 06:00 PM 1
Share

Probably because you are negating the Lerped value, although I can see other potential issues with this approach as well. You meed to set your target rotation to the correct "negative yaw" position rather than negating the returned value. Try:

 relativeRot = Vector3.Lerp(
                     rudders[0].localRotation.eulerAngles,//first parameter
                     -(maxRot * ruddersVectorRot),//second parameter
                     Time.deltaTime*5);

This will only work if "-(maxRot * ruddersVectorRot)" gives the correct target rotation for the max negative yaw position.

You should ideally use Quaternions directly to handle rotations. Just setup the max positive and max negative quaternion rotations then Lerp or Slerp using rudders[0].localRotation. You can get the reference Quaternions using Quaternion.Euler or Quaternion.AngleAxis, for example:

 Quaternion maxPositiveRotation = Quatnerion.AngleAxis(maxRot, Vector3.up);
 Quaternion maxNegativeRotation = Quatnerion.AngleAxis(-maxRot, Vector3.up);
avatar image Huacanacha · Aug 21, 2014 at 06:01 PM 1
Share

Re you're edit: in that case "-(maxRot * ruddersVectorRot)" doesn't give the correct target rotation for negative yaw. Use quaternions ins$$anonymous$$d as I discuss above.

It's only really advisable to manipulate rotation vectors if you're modifying a single axis... in which case you might as well just Lerp the float representing the axis rotation rather than the Vector3. Anything dealing with multiple axis rotations should use Quaternions otherwise you'll be dealing with things like Gimbal lock, order or axis rotations (meaning you can't necessarily change one axis without affecting the others) etc.

avatar image joseques · Aug 21, 2014 at 06:56 PM 0
Share

@Huacanacha using Quaternion.Lerp with the maxPositiveRotation and maxNegativeRotation it only makes it glitch the rotation and it's even worse than using the Vector3.Lerp with euler angles. Also i've tried Lerping only the axis that need to be rotated but stills like the beggining, works like a charm on positive rotation but it's a disaster with a negative value

avatar image Huacanacha · Aug 21, 2014 at 07:17 PM 0
Share

Add some debugs. What is "maxRot * ruddersVectorRot"? Is the standard rotation 0 degrees around the Y axis (i.e. you start at [0,0,0])?

How do you assign the relativeRot back to the models transform? Are you confusing localRotation with rotation anywhere? Add some more of the relevant parts of your script to your question so we can see exactly how this is used.

avatar image joseques · Aug 21, 2014 at 07:41 PM 0
Share

maxRot it's the max rotation ammount. Value:(float)45

ruddersVectorRot it's the axis for the rotation (So I can change it from the inspector) Value: (0,1,0) (It may change)

The standard rotation it's completely 0 on three axis

I assign the relativeRot to the eulerAngles of a new Quaternion and then I assign it to the Transform

 tempRotation.eulerAngles = relativeRot;
 rudders[0].localRotation = tempRotation;

3 Replies

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

Answer by joseques · Aug 22, 2014 at 01:35 AM

I've found the answer when I was coming back to home.

I've rotated the Transform so the rotation is 0,180,180. That way I can substract or add the rotations without using negative degrees.

Thanks @Huacanacha and @bustedkrutch for trying to help.

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 Huacanacha · Aug 22, 2014 at 02:15 AM 1
Share

No problem. I'm glad you found a solution!

avatar image
15
Wiki

Answer by BakonGuy · Mar 05, 2015 at 05:59 PM

The accepted answer here is just a workaround, and will not work in all scenarios.

The real problem here is that there is not such a thing as a negative angle, only 0 degrees through 360 degrees. So when a negative angle is set, it is converting itself to a proper angle.

Because of this, you cannot cross zero

For example, you have an Angle of 10 and you are trying to Lerp to -10.

  1. You start a lerp from 10 to -10

  2. Said lerp gets to -1

  3. Negative 1 is converted to 359 degrees

  4. Lerp now tries to Lerp from 359 to -1, causing the spin

Mathf has a LerpAngle function which will treat the value as an angle in degrees, so it will for example Lerp from 350 to 10 by increasing value instead of decreasing. Using this function you can create a AngleLerp function to replace Vector3.Lerp like so

     //Properly Lerp between two angles
     Vector3 AngleLerp(Vector3 StartAngle, Vector3 FinishAngle, float t)
     {        
         float xLerp = Mathf.LerpAngle(StartAngle.x, FinishAngle.x, t);
         float yLerp = Mathf.LerpAngle(StartAngle.y, FinishAngle.y, t);
         float zLerp = Mathf.LerpAngle(StartAngle.z, FinishAngle.z, t);
         Vector3 Lerped = new Vector3(xLerp, yLerp, zLerp);
         return Lerped;
     }

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

Answer by bustedkrutch · Aug 21, 2014 at 07:10 PM

I'm not one of the experts, I'm just a hack, but I would try removing the ( - ) from -(Vector3.Lerp.. If the rotation is relative, perhaps when the second parameter is negative the direction is already taken care of.

If I'm wrong I'd hope you don't down vote me for trying.

Good luck.

Comment
Add comment · Show 3 · 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 joseques · Aug 21, 2014 at 07:12 PM 0
Share

I've tested it already, so many ways and still don't working. I know you're trying to help so I'll not downvote you.

avatar image bustedkrutch · Aug 21, 2014 at 07:56 PM 1
Share

THAN$$anonymous$$ YOU!!! I think next time I'll post in the safety of the "comments" section lol

avatar image joseques · Aug 22, 2014 at 12:21 AM 0
Share

Haha, sure

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

23 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

Related Questions

How to add rotation to current rotation over time? 2 Answers

Two rotations at the same time, on the same axis. 1 Answer

How to smoothly rotate to certain directions using input axis 1 Answer

Rotation jumping to a different rotation after rotating a certain amount 0 Answers

lerp between two xyz rotation values 2 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