- Home /
LookTo Rotation Mouse Drag
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.
Radial drag has been posted a few times UA, so either search out the answers on UA, or alternately post your code and get suggestions on how to solve the problem.
The basic idea is to get the rotation when the mouse go down and then to offset other rotations during the drag by that amount.
Yeah that's what I was going for, but I can't seem to find a function that executes that (the offset). I've tried subtracting the starting rotation from the current rotation, but you can't subtract quaternions.
Answer by robertbu · Oct 15, 2014 at 07:10 PM
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;
}
Robert I love you. I don't really understand the formula yet, but it works and I'm going to take my time to dissect it. Thanks man!
Answer by unimechanic · Oct 15, 2014 at 04:22 PM
I've tried subtracting the starting rotation from the current rotation, but you can't subtract quaternions
You can subtract the Euler angles:
http://docs.unity3d.com/ScriptReference/Quaternion-eulerAngles.html
And this could be useful when working with angles:
http://docs.unity3d.com/ScriptReference/Mathf.Repeat.html
http://docs.unity3d.com/ScriptReference/Mathf.DeltaAngle.html
http://docs.unity3d.com/ScriptReference/Mathf.LerpAngle.html
http://docs.unity3d.com/ScriptReference/Mathf.MoveTowardsAngle.html
http://docs.unity3d.com/ScriptReference/Mathf.SmoothDampAngle.html
RotateTo.eulerAngles = Quaternion.LookRotation(Vector3.forward,hit.point).eulerAngles - RotateFrom.eulerAngles;
transform.rotation = RotateTo;
This is my current code, but it doesn't seem to work the way I imagined it. Shouldn't this produce a drag affect?
Your answer
![](https://koobas.hobune.stream/wayback/20220613165102im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Using quaternion for mouse movement? 3 Answers
Quaternion - Euler Angles, weird variable swapping when converting 2 Answers
Keep objects current rotation, with Input Mouse X 0 Answers
allows users to view 360º content without a VR headset 0 Answers
How do I rotate Z axis based off mouse click and drag? 2 Answers