- Home /
Top down look rotation snaps when looking left, right, and back
Sure enough, these are the 3 directions the camera sees. The camera is moved back and up and angled down at 60 degrees. so it makes sense that only left right and forward are snapping. However, I have no idea why they are snapping in the first place. Heres my code:
Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast (ray, out hit, 50, rayMask))
{
Vector3 point = new Vector3 (hit.point.x, transform.position.y, hit.point.z);
transform.LookAt (point);
}
So basically when I rotate a certain amount, it rotates another 20 or so degrees in a "snappy" fashion. its only when I look left right and at the camera. I am sure only the plane is in the cast mask.
Answer by C_Randy · Oct 17, 2015 at 04:38 AM
I did a similar thing with an xbox controller turning the player.
Get the LookAt position you want, in your case: point. Now save your current rotation: var rot = transform.rotation.eulerAngles. Give the transform the new lookAt point like you have. Now Lerp the rotation to smooth out the turning. eg. transform.rotation = Quaternion.Lerp(target rotation, current rotation, speed/smoothness).
Well after adding the lerp, it doesn't seem to turn towards the camera at all. Does this have to do with that my camera looks down but at a 60 degree angle?
Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast (ray, out hit, 50, ray$$anonymous$$ask))
{
Vector3 point = new Vector3 (hit.point.x, transform.position.y, hit.point.z);
Quaternion oldRotation = transform.rotation;
transform.LookAt (point);
Quaternion newRotation = transform.rotation;
transform.rotation = oldRotation;
transform.rotation = Quaternion.Lerp (transform.rotation, newRotation, rotationSpeed * Time.deltaTime);
}
This is what I used for a controller input:
if (Input.GetAxis(controls.RightStickX) != 0f || Input.GetAxis(controls.RightStickY) != 0f)
{
Vector3 lookAtPosition = transform.position + new Vector3(Input.GetAxis(controls.RightStickX), 0.0f, Input.GetAxis(controls.RightStickY));
Vector3 rotation = transform.rotation.eulerAngles;
transform.LookAt(lookAtPosition);
transform.rotation = Quaternion.Lerp(Quaternion.Euler(rotation), transform.rotation, 0.2f);
}
Your answer
Follow this Question
Related Questions
SphereCast help 1 Answer
LookAt To Only Rotate on Y Axis 2 Answers
Make character look at mouse position, not world position 3 Answers
[HELP]Object Look At Raycast position 2 Answers
Rotate object to lookAt point and be perpendicular to the ground 1 Answer