- Home /
How to Reset Quaternion/Rotation On 3D Object After Dragging Mouse?
Hello, I have an issue with resetting my gameobject's rotation. The rotation is based off mouse drag with restrictions. I have it to when the mouse is released the object returns to its starting rotation. But when I re-drag the object again the object rotation starts from where it was left off at from the previous drag. When I need it to drag from its starting rotation. I've tried many different methods nothing seems to work.
public Camera cam;
public float speed, minEulerX, maxEulerX, minEulerY, maxEulerY;
float eulerX, eulerY, rotateY, rotateX, inputX, inputY;
Quaternion startrot;
bool touch;
void Start()
{
startrot = transform.rotation;
}
void Update()
{
if (Input.GetMouseButtonDown(0))
{
Ray ray = cam.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out RaycastHit hit, Mathf.Infinity, Object))
{
inputX = Input.mousePosition.x;
inputY = Input.mousePosition.y;
touch = true;
}
}
if (Input.GetMouseButton(0))
{
if (touch)
{
rotateX = -(Input.mousePosition.y - inputY) * speed;
rotateY = -(Input.mousePosition.x - inputX) * speed;
eulerX = Mathf.Clamp(eulerX + rotateX, minEulerX, maxEulerX);
eulerY = Mathf.Clamp(eulerY + rotateY, minEulerY, maxEulerY);
transform.rotation = Quaternion.Euler(-eulerX, eulerY, 0) * startrot;
inputX = Input.mousePosition.x;
inputY = Input.mousePosition.y;
}
}
if (Input.GetMouseButtonUp(0))
{
transform.rotation = startrot;
touch = false;
}
}
Answer by stalwart5033 · May 09 at 10:56 PM
Set zero the eulerX and eulerY values after rotating
Answer by Hawaii_Dev · Apr 28 at 07:27 AM
You have to set your inputX and inputY to zero so to not start the offset at a weird angle. Insert into your if(Input.GetMouseButtonUp(0)) function: inputX = 0; inputY = 0;
hello, I tried your recommended answer & im getting the same results for some reason
Your answer
Follow this Question
Related Questions
How to smoothly rotate an object on only two axes? 2 Answers
Snap and align two cubes 1 Answer
How get the rotation of an GameObject? 3 Answers
Rotate object to target position 1 Answer
Clamped Rotate 3D Object on Y and Z axis only with OnMouseDrag() 2 Answers