Quantcast
Channel: Questions in topic: "quaternion.slerp"
Viewing all articles
Browse latest Browse all 133

Quaternion.Slerp Does Not Rotate Object

$
0
0
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; } }

Viewing all articles
Browse latest Browse all 133

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>