- Home /
Gradually reduce mouse sensitivity as we approach an angle
I am working on a 3D game with a first person controller and I have been trying (without any luck) to find a way to gradually reduce my camera rotation speed to a stop, as I approach a specified angle.
Basically, the behaviour I am looking for, is to be able to rotate the camera on the X and Y axis with the mouse input, but the farther I get from the center of the screen, the slower the movement becomes, until it gets completely clamped.
I want to be able to clamp my rotation on both axes, but I also want the camera to gradually get slower (or the sensitivity to get lower) as we approach the clamp value.
I don't have a strong knowledge of math, which I believe is why I am failing to make this work..
Here's an example of what I've tried before :
private void TestRotateClamp()
{
if (Input.GetAxis("Mouse X")!= 0 || Input.GetAxis("Mouse Y") != 0)
{
Quaternion camRot = transform.localRotation;
float maxAngle=7;
float angle=Mathf.Abs(Quaternion.Angle(camRot, startRotation)); // startRotation is the center of the screen in the example
float ratio = Mathf.Max(maxAngle-angle,0)/
float yRot = Input.GetAxis("Mouse X") * sensitivity * ratio;
characterTargetRot *= Quaternion.Euler (0f, yRot, 0f);
cameraTargetRot *= Quaternion.Euler (-xRot, 0f, 0f);
transform.localRotation = Quaternion.Slerp (transform.localRotation, characterTargetRot,damping*Time.deltaTime);
eyes.transform.localRotation = Quaternion.Slerp (eyes.transform.localRotation, cameraTargetRot,damping*Time.deltaTime);
previousMousePosX = Input.GetAxis("Mouse X");
previousMousePosY = Input.GetAxis("Mouse Y");
}
}
Here the camera does slow down towards the angle and stops, but since the ratio of the sensitivity is based on the value of the angle, it can never come back once it hits the 0 value..
Any help here would be greatly appreciated !
Your answer