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
2
Question by rocket350 · Feb 18, 2014 at 08:10 PM · c#lerprotate object

How can I lerp an objects rotation?

I want to rotate a cube smoothly with the lerp function from its current rotation to a value which would be
x: 0
y: 345 z: 0

How should I approach this? C#

Comment
Add comment · Show 4
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 DajBuzi · Feb 18, 2014 at 08:27 PM 0
Share
  private float speed, $$anonymous$$, max;
 // later in update or such
 float rot += speed * Time.deltaTime;
 rot = $$anonymous$$athf.Lerp($$anonymous$$, max, rot);
avatar image POLYGAMe · Feb 18, 2014 at 08:30 PM 1
Share

You're leaping between two floats, not rotations. Those floats would then need to be applied to the cube's transform/rigidbody rotation. Quaternion Lerp/Slerp would be the better option, methinks.

avatar image RudyTheDev · Feb 18, 2014 at 09:40 PM 0
Share

@DajBuzi: Yeah, that won't work, because it doesn't loop; the correct function would be $$anonymous$$athf.LerpAngle(); But then you need to cache the transform.eulerAngles to begin with.

avatar image DajBuzi · Feb 19, 2014 at 08:23 AM 1
Share

@$$anonymous$$TheDev if you combine this with switch() or else-if statement then it will work. But in the form as i've posted, you are absolutely right - it wont loop.

4 Replies

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

Answer by RudyTheDev · Feb 19, 2014 at 08:38 PM

The simplest way is Quaternion.Lerp as answered before and you can convert your target/desired rotation euler angle rotation into quaternion with Quaternion.Euler().

Alternatively, if you want to work with euler angles directly, you can access tranform.eulerAngles and lerp with Mathf.LerpAngle() each angle individually. Note, however, that there are multiple ways in euler angles to represent certain 3D rotations (gimbal lock), so you will need to manually keep track of your current and desired target rotation, lerp that value, and assign it to live transform (otherwise the "real" euler angles can suddenly jump/change at certain overlapping angles). Something like:

 public class RotateMe : MonoBehaviour
 {
     public Vector3 targetAngle = new Vector3(0f, 345f, 0f);
 
     private Vector3 currentAngle;
 
     public void Start()
     {
         currentAngle = transform.eulerAngles;
     }
 
     public void Update()
     {
         currentAngle = new Vector3(
             Mathf.LerpAngle(currentAngle.x, targetAngle.x, Time.deltaTime),
             Mathf.LerpAngle(currentAngle.y, targetAngle.y, Time.deltaTime),
             Mathf.LerpAngle(currentAngle.z, targetAngle.z, Time.deltaTime));
 
         transform.eulerAngles = currentAngle;
     }
 }

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 POLYGAMe · Feb 19, 2014 at 08:40 PM 1
Share

^ There ya go :)

avatar image Surprisejedi · Jul 27, 2015 at 10:17 PM 0
Share

Thank you so much! This helped me a lot. This definitely works!

avatar image KarlKarl2000 · Sep 17, 2017 at 06:41 AM 0
Share

@$$anonymous$$TheDev

Just wanted to say THAN$$anonymous$$ YOU!! This was what worked.. Quaternion.RotateTowards and other methods were crap.. causes the rotations to keep going and not stop. Quaternion.Lerp was unstable for me .. but your alternate method worked great. As long as I didn't go past some high number like "100" ..

Saved my life! Thanks again!

Sharin' is Caring!

avatar image
6

Answer by MadJohny · Feb 19, 2014 at 03:42 PM

As POLYGAMe said, uou will use Quaternion.Lerp: https://docs.unity3d.com/Documentation/ScriptReference/Quaternion.Lerp.html

But you have to change the code there, so:

 transform.rotation = Quaternion.Lerp (transform.rotation, new Quaternion.Euler(transform.rotation.x, 345f, tramsform.rotation.z), Time.deltaTime * speed);

Hope this helps, and if it does, accept the answer

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 ZeroKcm · Jun 01, 2018 at 01:14 PM 0
Share

Your example doesn't work because you change the value of transform.rotation each time you execute it.

avatar image
1

Answer by POLYGAMe · Feb 18, 2014 at 08:26 PM

https://docs.unity3d.com/Documentation/ScriptReference/Quaternion.Lerp.html

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 rocket350 · Feb 19, 2014 at 03:36 PM 0
Share

@POLYGA$$anonymous$$e I already checked that, but I want to lerp with euler angles. Exactly 345 degrees in y and none in x and z

avatar image POLYGAMe · Feb 19, 2014 at 07:53 PM 0
Share

You should just be able to lerp between two float amounts then, 0-345. Use that lerped float as your local euler y in update.

avatar image POLYGAMe · Feb 19, 2014 at 08:11 PM 1
Share

Actually, I was just writing out how to do it but then remembered in C# you can't access the euler angle axes individually. What's wrong with using quaternions, anyway?

avatar image
1

Answer by Surprisejedi · Jul 27, 2015 at 10:20 PM

If you are still working on this Rocket, I had the same issue that you had and all it was was this

 private Vector3 currentAngle;
 
 public void Start()
 {
     currentAngle = transform.eulerAngles;
 }
 
 public void Update()
 {
         currentAngle = new Vector3(currentAngle.x, currentAngle.y, 90);
     
     transform.eulerAngles = currentAngle;
     }

Hopefully this helps you out!

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 rocket350 · Jul 28, 2015 at 09:00 AM 0
Share

Thanks but already got a solution from rudyTheDev

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

26 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Question on Forcing a Specific Rotation 1 Answer

Player movement starts after a few seconds after pressing the button 1 Answer

Why is my character rotating without input? 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