When our game's ships move around, mouse aiming causes them to shake around a lot. The body being aimed is moved kinematically. I think we've narrowed it down to this block of mouse tracking code.
You can see around 0:35 where I disable the mouse tracking code momentarily, the ship stops shaking.
public static Quaternion RotateToMouse(this Ship ship, Transform transform, float ShipRotateSpeed)
{
// Generate a plane that intersects the transform's position with an upwards normal.
Plane playerPlane = new Plane(Vector3.up, Vector3.zero);
// Generate a ray from the cursor position
Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
// Determine the point where the cursor ray intersects the plane.
float hitdist = 0.0f;
// If the ray is parallel to the plane, Raycast will return false.
if (playerPlane.Raycast (ray, out hitdist))
{
// Get the point along the ray that hits the calculated distance.
Vector3 targetPoint = ray.GetPoint(hitdist);
// Determine the target rotation. This is the rotation if the transform looks at the target point.
Quaternion targetRotation = Quaternion.LookRotation(targetPoint - transform.position);
// Smoothly rotate towards the target point.
transform.rotation = Quaternion.RotateTowards(transform.rotation, targetRotation, ShipRotateSpeed);
transform.eulerAngles = new Vector3(0, transform.eulerAngles.y, 0);
}
return transform.rotation;
}
We've got no idea whats going wrong, any help will be greatly appreciated. thanks.
Quaternion.RotateTowards()
, you likely wantQuaternion.Slerp()
(link) to ensure you don't overshoot your target rotation. \$\endgroup\$RotateTowards
should not overshoot according to the specification,Slerp
produce a different effect that I presume undesirable for the purpose. \$\endgroup\$