Hi, I’ve got a problem that I can’t find anywhere. I have a disk that rotates by holding my mouse. The disk constantly points towards the mouse, which is good, however I want it to look like it’s being dragged. Meaning, if you click somewhere on the disk it doesn’t instantly point towards the mouse but only moves when the mouse moves too, from that point.
I fail to get the wished results, can someone help me?
I’m currently using a raycast to detect the mouse’s position and rotate my circle towards hit.point with LookRotation.
Here is a Quaternion solution to drag rotate an object. It assuming the camera is looking towards positive ‘z’ since it rotates around Vector3.forward.
#pragma strict
private var qStart : Quaternion;
function OnMouseDown() {
var dir = Input.mousePosition - Camera.main.WorldToScreenPoint(transform.position);
var angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg;
qStart = Quaternion.Inverse(Quaternion.AngleAxis(angle, Vector3.forward)) * transform.rotation;
}
function OnMouseDrag() {
var dir = Input.mousePosition - Camera.main.WorldToScreenPoint(transform.position);
var angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg;
transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward) * qStart;
}
I’ve tried subtracting the starting rotation from the current rotation, but you can’t subtract quaternions
You can subtract the Euler angles:
And this could be useful when working with angles: