Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 al-gco · Jul 11, 2019 at 06:31 PM · rotationquaternionlerpslerpeuler

How can I use lerp to rotate an object multiple times?

My situation is this:

I have an object whose rotation in Euler angles is (0, 12, 0) and I want to rotate it using a lerp function to eg (3600, 12, 0). That is to say, I want it to rotate around the x axis 10 times.

I want to use lerp because I want to use an animation curve to control the rate of the spin - starting slowly, speeding up, finishing slowly. I can't use a sin curve or similar ease function as I need something more idiosyncratic, hence an animation curve I can edit. Also the end rotation and duration of the spin are set in code as they vary, so I can't use an animation. And to make it super complicated, the start rotation also varies!

My first thought was a simple Vector3.lerp between the euler angles, but this doesn't work - as Unity uses Quaternions internally it comes out wonky at the end.

I then thought I would convert the start and end positions into Quaternion values and slerp between them, but that only rotates once because that's all that Quaternions can represent.

So now I have this monstrosity:

 while (timer < spinTime)
 {
     transform.localRotation = Quaternion.Euler(Vector3.Lerp(startRot, endRot, spinCurve.Evaluate(timer / spinTime)));
   
     yield return null;
   
     timer += Time.deltaTime;
 }



Which also doesn't work because even though the start rotation and end rotation as Euler angles have the same y and z values, when converted to Quaternions they don't, so it spins around multiple axes.

I also tried manually forming the start rotation value to force the z value to remain as 0:

 Vector3 startRot= Quaternion.Euler(transform.localEulerAngles.x, transform.localEulerAngles.y, 0).eulerAngles;
  

But you guessed it, even though transform.localEulerAngles.z == 0, the startRot this generates does not match this:

 Vector3 startRot= Transform.localEulerAngles;
  

So when the lerp starts it jumps to a different starting position.

Help!

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

2 Replies

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

Answer by al-gco · Jul 11, 2019 at 09:04 PM

Solved here: https://forum.unity.com/threads/help-with-using-lerp-to-rotate-3d-object-multiple-times.708953/

I solved my problem! Not in a generally applicable way, so probably not too helpful to others, but just in case...

In fact the first thing I tried (Vector3.lerp between the Euler angles) was the correct method. The problem was that I was creating end rotation values based on what I was seeing in the inspector, but when I called transform.localEulerAngles I was getting quite different numbers. As my start values were not what I expected I was getting unwanted results.

My solution is to not get the start values from transform.localEulerAngles, but to deduce what they should be based on the game state, and use that as the initial lerp value. Luckily my project makes this pretty easy as there are not many possible starting positions.

The lesson is that transform.localEulerAngles should be used with caution as it does not provide a predictable result, and it will probably not match what is visible in the inspector.

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
0

Answer by I_Am_Err00r · Jul 11, 2019 at 09:01 PM

Try messing with this free tool that does really fantastic things with animations and movement during runtime: https://assetstore.unity.com/packages/tools/animation/dotween-hotween-v2-27676

It is probably one of the most popular assets, and the pro version is pretty cheap: https://assetstore.unity.com/packages/tools/visual-scripting/dotween-pro-32416

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

145 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

Related Questions

Lerp 180 degrees while holding B and lerp back 180 degrees when let go of B. 2 Answers

Quaternion.Slerp and Quaternion.Euler problem 1 Answer

Quaternion.Slerp problem... 1 Answer

Transition current camera rotation to 0,0,0 1 Answer

Changing rotation of Quaternion 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