I’m trying to use the below code to smoothly rotate an object to face the mouse cursor on the y axis. However, it only rotates to about 90 degrees regardless of where the cursor is.
How can I correct this?
//rotate to mouse position
float turnSmooth = 2.0f;
var targetPoint = Input.mousePosition;
var targetRotation = Quaternion.LookRotation(targetPoint - transform.position);
var targetAngle = targetRotation.eulerAngles.y;
var currentAngle = transform.eulerAngles.y;
var upAngleSmooth = currentAngle + turnSmooth;
var downAngleSmooth = currentAngle - turnSmooth;
//Turn conditions
if (targetAngle > upAngleSmooth){
if (targetAngle - currentAngle > 180.0)
{
rb.AddTorque(new Vector3(0, -1, 0) * turnSpeed * Time.deltaTime);
}
else if (targetAngle - currentAngle < 180.0)
{
rb.AddTorque(new Vector3(0, 1, 0) * turnSpeed * Time.deltaTime);
}
}else if (targetAngle < downAngleSmooth){
if (currentAngle - targetAngle > 180.0)
{
rb.AddTorque(new Vector3(0, 1, 0) * turnSpeed * Time.deltaTime);
}
else if (currentAngle - targetAngle < 180.0)
{
rb.AddTorque(new Vector3(0, -1, 0) * turnSpeed * Time.deltaTime);
}
}
That helped, thank you!
– ayockel90