Hello
I have a problem with player’s rotation. I have a mouse look script, which allows a rotation from side to side. Also, I’ve wrote a script, which allows me to focus on an enemy by holding right mouse button. This script disables the free rotation possibility and rotates the player towards the enemy. Everything works fine until I release the right mouse button. It results in player rotating suddenly to a different angle. I figured out the problem is that a RotationX variable forces the player to rotate to a different angle after enabling the free rotation.
Here are the fragments of the mouse look script:
//variables:
public float SensitivityX= 15F;
public float MinX = -360F;
public float MaxX = 360F;
public float RotationX = 0F;
Quaternion OriginalRotation;
public bool DisableRotation;
//code in the LateUpdate:
if (!DisableRotation) {
RotationX += Input.GetAxis("Mouse X") * SensitivityX;
RotationX = ClampAngle(RotationX, MinX, MaxX);
Quaternion xQuaternion = Quaternion.AngleAxis(RotationX, Vector3.up);
transform.localRotation = OriginalRotation * xQuaternion;
}
-------------------------------------------
public static float ClampAngle(float angle, float min, float max)
{
if (angle < -360F)
angle += 360F;
if (angle > 360F)
angle -= 360F;
return Mathf.Clamp(angle, min, max);
}
And here is the fragment of the script, that disables the free rotation and puts focus on the enemy:
void RotateTowardsTarget (Transform targetBody)
{
if (!mouseLook.DisableRotation)
mouseLook.DisableRotation = true;
Vector3 target = targetBody.position - transform.position;
target.y = 0;
var rot = Quaternion.LookRotation(target);
transform.rotation = Quaternion.Slerp(transform.rotation, rot, Time.deltaTime * RotationSpeed);
}
I’ve tried to find any thread, which could help but without a success. Can you please help me?
Regards!