I need mouse/touch drags to translate to rotation of an object around its z-axis. So, if the player drags around in circles, the object should continue rotating around the z-axis properly, following the mouse/touch movement. I’ve pieced together a little code from various posts, but it’s not quite right.
I think I also need to move things out of OnMouseDrag, since I want the player to use screen drags instead of clicking on the object itself.
void OnMouseDown()
{
_isRotating = true;
_mouseReference = Input.mousePosition;
}
void OnMouseDrag()
{
if(_isRotating)
{
// offset
_mouseOffset = (Input.mousePosition - _mouseReference);
Debug.Log("ref: " + _mouseReference + "; offset: " + _mouseOffset);
// apply rotation
_rotation.z = (_mouseOffset.x + _mouseOffset.y) * Sensitivity;
// rotate
transform.Rotate(_rotation);
// store mouse
_mouseReference = Input.mousePosition;
}
}
void OnMouseUp()
{
_isRotating = false;
}
The only rotation that works right now is when on the right or bottom of the object. The rotation also doesn’t work once the angle moves into the top or left side of the object. I need it to be continuous.