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
0
Question by glassman3d · Apr 04, 2013 at 03:20 AM · rotationvector3lerp

lerp between two xyz rotation values

Hi there

Apologies for the noobiness of this question,

I want to create a lerp ( or even better a smoothstep) between two rotation values. My code below creates a very sudden transition over one frame. Duration is a float with val 20f

many thanks in advance!

     void Update () {
         
         if (armstate == 1) { 
         
             transform.eulerAngles = Vector3.Lerp (startRot,endRot,duration);
             
             armstate = 0;
         }
         
         
         if (armstate == 2) { 
         
             transform.eulerAngles = Vector3.Lerp (endRot,startRot,duration);
             armstate = 0;
         }
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
1
Best Answer

Answer by robertbu · Apr 04, 2013 at 04:43 AM

The final parameter of the Lerp() is a value between 0 and 1 inclusive. A standard way of using Lerp() is to create a timer and use the timer in the final parameter calculation. Here is your code with some changes. It probably doesn't do want you want, but it does show Lerp() in action. Attach this script to a Cube object and hit play.

 using UnityEngine;
 using System.Collections;
  
 public class LerpDemo : MonoBehaviour {
     
     Vector3 startRot = new Vector3(-30.0f,-30.0f,-30.0f);
     Vector3 endRot   = new Vector3(60.0f, 60.0f, 60.0f);
     
     int armstate = 0;
     float duration = 20.0f;
     float timer = 0.0f;
     
     void Update () {
         
         timer += Time.deltaTime;
         if (timer > duration) {
             armstate = (armstate + 1) % 2;
             timer = 0.0f;
         }
  
        if (armstate == 0) { 
            transform.eulerAngles = Vector3.Lerp (startRot,endRot,timer/duration);
        }
  
        if (armstate == 1) { 
          transform.eulerAngles = Vector3.Lerp (endRot,startRot,timer/duration);
        }
     }
 }
Comment
Add comment · Show 2 · 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 glassman3d · Apr 09, 2013 at 12:21 AM 0
Share

thanks that was very helpful! starting to get my head around it now!

avatar image rbrady · Jan 22, 2016 at 07:13 PM 1
Share

This class is great but I would make one change. Eulers are awful at lerp-ing since order of operation matters, ie, xyz, yzx, zxy, etc. The rotation can often look really strange. Gimble lock can happen too. Eulers work fine for a while, then at certain angles it will spaze. You can keep your rotations in euler but lerp them in Quaterion.

So

Vector3 startRot = new Vector3(-30.0f,-30.0f,-30.0f);

Becomes

Quaternion startRot = Quaternion.Euler( new Vector3(-30.0f,-30.0f,-30.0f) );

And

transform.eulerAngles = Vector3.Lerp (startRot,endRot,timer/duration);

Becomes

transform.rotation = Quaternion.Lerp(startRot,endRot,timer/duration);

Its really the same idea, but this will lerp in a mathematical space that is much more reliable.

avatar image
0

Answer by jamesflowerdew · Jan 22, 2016 at 05:29 PM

to lerp from grot to a target angle named vRot::

gRot = new Vector3(Mathf.LerpAngle(gRot.x,vRot.x,pFrame),Mathf.LerpAngle(gRot.y,vRot.y,pFrame),Mathf.LerpAngle(gRot.z,vRot.z,pFrame));

works for me :)

Comment
Add comment · Show 2 · 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 callen · Jan 22, 2016 at 06:11 PM 1
Share

It should be simpler (and less error-prone) to just use quaternions though:

gRot = Quaternion.Lerp(Quaternion.Euler(gRot), Quaternion.Euler(vRot)).eulerAngles;

avatar image jamesflowerdew callen · Jan 28, 2016 at 11:27 AM 0
Share

yup I agree, your answer is "the answer"

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

13 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

Related Questions

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

Issues with Lerps 2 Answers

Lerping euler angles make entire spin 3 Answers

How to know if a gameobject is rotating? 1 Answer

How would I smooth out a Quaternion? 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