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 /
avatar image
0
Question by Double_D · Jul 06, 2016 at 07:17 PM · c#scripting problemquaternionlerp

Quaternion.Lerp is not behaving as expected

I'm using the below script to try and turn an object 180° around its local y-axis without stopping until it's either facing right, or left on the screen, and all within the time frame of the variable rotationTime. Because I'm driving the Lerp function with an alternating increment/decrement time variable, the user should be able to interrupt the current rotation and begin turning back to face the previous direction.

The problem is that no matter what I try, the rotation appears to occur almost instantly, and appears to ignore any change in the variable rotationTime instead of being driven by the currentRotationTime /rotationTime parameter in the Lerp function. I've been over this more times than I can count, and I'm not seeing where I've gone wrong.

Just about the only thing I can think of, is that it's due to my changing the lookRotation on each iteration, instead of simply giving it two static rotation values and telling it to Lerp between them. That seems like that would be a pretty severe limitation of Quaternions though and that it shouldn't matter, because the Lerp is controlled by time, and should simply recalculate the new rotation based on all it's current parameters.

Any help is greatly appreciated. Thanks! using UnityEngine;

 public class PlayerInput : MonoBehaviour {
 
     [Range(0f, 2f)]
     public float rotationTime;
 
     private Quaternion pendingRotation;
     private float currentRotationTime;  
     private bool isRotating;
 
     private Vector3 directionVector;
     private Vector3 previousDirectionVector;
 
 void Start () {
         
         directionVector = Vector3.right;
         previousDirectionVector = Vector3.right;
         pendingRotation = Quaternion.identity;
         currentRotationTime = 0.0f;
     }
 
 void Update () {
 
     // Get the right sticks input
     Vector3 rightStick = new Vector3(Input.GetAxisRaw("RightStickX"), Input.GetAxisRaw("RightStickY"), 0.0f);
     
     // Is there any new input?
     if (rightStick != Vector3.zero) {
         // Get the input and normalize it to a vector
         directionVector = rightStick.normalized;
 
     // User says we should be facing left?
     if (directionVector.x < 0f) {
         
         // This is for another part of the script that isn't included here. 
         // Magnitde shouldn't matter, just direction, so clamp the value to -1.0
         //so that we're only concerned about the y-value
         directionVector.x = -1.0f;
         
         // Initiate a new rotation, or continue processing a previous rotation?
         if (previousDirectionVector.x > 0.0f) {
                 isRotating = true;                            
         }
     }
     
     // User says we should be facing right
     else if (directionVector.x > 0f) {
     
         // This is for another part of the script that isn't included here.
         // Magnitde shouldn't matter, just direction, so clamp the value to -1.0
         // so that we're only concerned about the y-value
         directionVector.x = 1.0f;
         
         // Initiate a new rotation, or contine processing a previous rotation?
         if (previousDirectionVector.x < 0.0f) {
                 isRotating = true;                            
         }
     }
         // Get a copy of this loops input vector for the next loop
         previousDirectionVector = directionVector;
     }
 
     // Begin processing a new rotation, or continue processing an existing rotation
     if (isRotating) { 
         Quaternion tempRotation1 = Quaternion.identity;
         Quaternion tempRotation2 = Quaternion.identity;         
          
         // If the player should be facing right, decrement the numerator to zero
         if (directionVector.x == 1.0f) { 
             currentRotationTime -= Time.deltaTime;
             tempRotation1 = Quaternion.LookRotation(directionVector, Vector3.up);
             tempRotation2 = transform.rotation;
         }
         else { // The player should be facing left, increment the numerator to rotationTime
             currentRotationTime += Time.deltaTime;
             tempRotation1 = transform.rotation;
             tempRotation2 = Quaternion.LookRotation(directionVector, Vector3.up);
         }
         
         // Clamp the time values so that we end facing exactly right/left
         currentRotationTime = Mathf.Clamp(currentRotationTime, 0.0f, rotationTime);
         
         // With currentRotationTime of 0.0f, we should get tempRotation1
         // With currentRotationTime == rotationTime, we should get tempRotation2
         pendingRotation = Quaternion.Lerp(tempRotation1, tempRotation2, currentRotationTime / rotationTime);
 
         // There are no more rotations to process, set the flag
         if ((currentRotationTime >= rotationTime) || (currentRotationTime <= 0.0f)){ 
             isRotating = false;
         }
     }
 
     // Apply the rotation to the object
     transform.localRotation = pendingRotation; 
 }
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by mroel · Feb 09, 2019 at 12:33 PM

Hi Double_D, did yo figure out a solution to this? I'm having the same issue.

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

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

The vision of a camera (displayed as a 3D Cone) needs to rotate towards the player 1 Answer

Lerp Rotation C# 3 Answers

Quaternion.Lerp shaky while in coroutine? 1 Answer

better way to rotate instead Coroutine 1 Answer

Rotation jumping to a different rotation after rotating a certain amount 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