Changing from x-axis LookAt to free x-axis mouse rotation results in a bug

Hello :slight_smile:

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? :slight_smile:
Regards!

One idea: at the moment you release the mouse button, maybe hot-monkey-patch (adjust) the RotationX variable to take out any pending offset, or bring it back to where you want it to be? This is commonly done with look code as the finger goes down: you zero out the offset.

Yeah, I’ve tried to do something like that, the problem is I am unable to adjust it to the value I want it to be, as I don’t really know what value it should be to remove the sharp rotation :smile: I tried to write the mouse look script in a way I could control it more easily, although the one I have is the only way that actually works for me :confused:

See if you can use Debug.Log() on the values to identify what is happening. Either the value that it was when you first started is being restored, or there is some other offset. If you print the angle every frame and see how much it “snaps” by you can perhaps understand what is going on over time.

Ask questions like: is the snap greater or lesser based on how much further you look? Or is it random? etc.

EDIT: based on this you can start to reason about the code and decide what calculations need to be adjusted. Without doing the experiments, you won’t really know what to tweak.