- Home /
Quaternion.Slerp Does Not Rotate Object
In Unity 2020, I have an object that I want the user to be able to drag and rotate in all three axes, then release the mouse button and have it "float" back to its original rotation. It cannot be moved with a mouse drag, just rotated.
I'm trying to use Quaternion.Slerp, but even though debug lines print to the console just before and after the Quaternion.Slerp line, it doesn't return to its original rotation.
In my Start() I get the original rotation of the object and save that in a variable. Then OnMouseUp I check to see if the object's current rotation is equal to the original. If not, then I want to use Quaternion.Slerp to rotate it from its current position to the original position over time.
Can anyone tell me what I'm doing wrong? - Below is my code: using System.Collections; using System.Collections.Generic; using UnityEngine;
public class userRotateObject_mouse : MonoBehaviour {
public Vector3 mPrevPos = Vector3.zero;
public Vector3 mPosDelta = Vector3.zero;
public float rotationSpeed = 0.2f;
public float retRotationSpeed = 5.0f;
private Quaternion originalRotation;
private Vector3 mOffset;
private float mZCoord;
void Start()
{
originalRotation = transform.rotation;
}
//After rotating the object with the mouse the user can release to let the object seek its original rotation. void OnMouseUp() {
if (deviceShouldTakeInitialPos && (transform.rotation.x != originalRotation.x || transform.rotation.y != originalRotation.y || transform.rotation.z != originalRotation.z) )
{
Debug.Log("Inside the IF");
transform.rotation = Quaternion.Slerp(transform.rotation, originalRotation, Time.deltaTime * retRotationSpeed);
// StartCoroutine(waitForTime());
Debug.Log("Exiting the IF");
}
}
//User can click/drag on the object to rotate in all three axes. void OnMouseDrag()
{
mPosDelta = Input.mousePosition - mPrevPos;
if (Vector3.Dot(transform.up, Vector3.up) >= 0)
{
transform.Rotate(transform.up, -Vector3.Dot(mPosDelta, Camera.main.transform.right), Space.World);
}
else
{
transform.Rotate(transform.up, Vector3.Dot(mPosDelta, Camera.main.transform.right), Space.World);
}
transform.Rotate(Camera.main.transform.right, Vector3.Dot(mPosDelta, Camera.main.transform.up), Space.World);
mPrevPos = Input.mousePosition;
}
}
Also, I don't know what's up with the screwy formatting of the question. Sorry about that.
Answer by xxmariofer · Aug 26, 2020 at 11:51 AM
on mouse up only gets called once, so it will not get reset completly change the code to this
void OnMouseUp() {
StartCoroutine(ReturnRotation());
}
IEnumerator ReturnRotation()
{
while (deviceShouldTakeInitialPos && (transform.rotation.x != originalRotation.x || transform.rotation.y != originalRotation.y || transform.rotation.z != originalRotation.z) )
{
transform.rotation = Quaternion.Slerp(transform.rotation, originalRotation, Time.deltaTime * retRotationSpeed);
yield return null;
}
}
Ok, that basically worked!
It gave an error on the ReturnRotation function (Cannot be an iterator), so I changed: void ReturnRotation() to: private IEnumerator ReturnRotation() and that did the trick. Thank you!
Your answer
Follow this Question
Related Questions
Quaternion Slerp 180 Away from target 1 Answer
few question about quaternion slerp 3 Answers
How do you check if Quaternion.Slerp has finished 5 Answers
How do I make an object face forward while it is orbiting another object? 1 Answer
By-pass the 'shortest route' aspect of Quaternion.Slerp 1 Answer