I am trying to find a way so that the player can drag and rotate an object freely using the mouse (click to view scene). As you can see the script to move the object works just fine, the problems come when you want to rotate it. In fact it works the firts time that you try to rotate the object. If you release the mouse and try again, the object snaps back to its initial rotation before being rotated by the mouse again. I don't want that. I want it to continue the rotation from where it is. Here's my code so far:
private void OnMouseDown()
{
worldPositionA = Camera.main.ScreenToWorldPoint(Input.mousePosition);
}
void Update()
{
worldPositionB = Camera.main.ScreenToWorldPoint(Input.mousePosition);
}
private void OnMouseDrag()
{
Vector3 relativePositionA = worldPositionA - parent.gameObject.transform.position;
Vector3 relativePositionB = worldPositionB - parent.gameObject.transform.position;
float angleBetween = Vector2.SignedAngle(relativePositionB, relativePositionA);
angleBetween += parent.transform.rotation.z;
parent.transform.rotation = Quaternion.AngleAxis(angleBetween, Vector3.back);
}
worldPositionA is the world position of the mouse when it starts dragging, worldPositionB is the updated mouse position. relativePosition (A and B) are the same but relative to the center of the object. I know they are correct because I have a raycast that points to those, and the angleBetween is correct too because I'm printing it in che console. What is wrong? Is there something I'm not thinking about?