- Home /
Gimbal-Lock with LookAt, RotateTowards, and LookRotation
So I have been trying to build a mouse look script. I was originally checking mouse position and rotating based on that, but it was rotating around the z axis which I do not want. When the player moves the mouse, I only want X and Y axis to rotate and have Z controlled elsewhere. So, what I did is I put a quad at the edge of my camera's boundary and cast a ScreenPointToRay. I then got that point and direction, and rotate towards it. But my issue is that I keep getting Gimbal Lock with all three of those functions. At the very least, it stops rotating at the -90 and 90 degree rotations for X rotation.
void TestMethod()
{
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit, Mathf.Infinity, layerMask))
{
Debug.DrawLine(ray.origin, hit.point);
}
//float distance = Vector3.Distance(centerScreen, mousePos);
mousePoint.position = hit.point ;
Vector3 direction = mousePoint.position - transform.position;
Quaternion targetPos = Quaternion.LookRotation(direction);
transform.rotation = targetPos;
}
UPDATE: I figured this entire issue out. No up-direction was set. Adding transform.up to the LookRotation fixed everything. It was rotating on the Vector3.up not the transform.up
Actually you were supplying an up vector. Be default you're passing Vector3.up as up vector. Of course when your look direction is also pointing up or down in worldspace, Vector3.up is pointless as up vector.