- Home /
Changing Mouse Sensitivity With HotKeys
I want to be-able to change the mouse sensitivity with the [ and ] keys. I'm using the First Person Controller so it already has a MouseLook.cs script in it but that thing is very confusing and i can barely work my way around it. Help Please :)
Answer by CodeMasterMike · Dec 20, 2012 at 06:20 AM
Well, it depends on how you are designing your MouseLook script. But lets say that you take the axis value from the mouse in x and y axis to move the camera, which always goes between 1 and -1, then you can add "sensitivity" to the x- and y-axis when the mouse moves.
Something like this:
private float mouseSensitivity = 1.0f;
Update()
{
if(Input.GetKeyDown("[") == true)
{
mouseSensitivity += 1.0f;
}
if(Input.GetKeyDown("]") == true)
{
mouseSensitivity -= 1.0f;
}
float translationInXaxis = Input.GetAxis("Mouse X") * mouseSensitivity;
float translationInYaxis = Input.GetAxis("Mouse Y") * mouseSensitivity;
CameraTransform.Translate(translationInXaxis, translationInYaxis, 0.0f);
}
So the camera translate more depending on how much sensitivity you add to the variable.
Good luck!
Answer by tw1st3d · Dec 20, 2012 at 06:23 AM
@script AddComponentMenu ("Camera-Control/Mouse Look")
enum RotationAxes { MouseXAndY = 0, MouseX = 1, MouseY = 2 }
var axes = RotationAxes.MouseXAndY;
var sensitivityX : float = 15;
var sensitivityY : float = 15;
var minimumX : float = -360;
var maximumX : float = 360;
var minimumY : float = -60;
var maximumY : float = 60;
var rotationX : float = 0;
var rotationY : float = 0;
private var originalRotation : Quaternion;
function Update () {
if (axes == RotationAxes.MouseXAndY) {
rotationX += Input.GetAxis("Mouse X") * sensitivityX;
rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
rotationX = ClampAngle (rotationX, minimumX, maximumX);
rotationY = ClampAngle (rotationY, minimumY, maximumY);
var xQuaternion = Quaternion.AngleAxis (rotationX, Vector3.up);
var yQuaternion = Quaternion.AngleAxis (rotationY, Vector3.left);
transform.localRotation = originalRotation * xQuaternion * yQuaternion;
}
else if (axes == RotationAxes.MouseX) {
rotationX += Input.GetAxis("Mouse X") * sensitivityX;
rotationX = ClampAngle (rotationX, minimumX, maximumX);
xQuaternion = Quaternion.AngleAxis (rotationX, Vector3.up);
transform.localRotation = originalRotation * xQuaternion;
}
else {
rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
rotationY = ClampAngle (rotationY, minimumY, maximumY);
yQuaternion = Quaternion.AngleAxis (rotationY, Vector3.left);
transform.localRotation = originalRotation * yQuaternion;
}
}
function Start () {
if (rigidbody)
rigidbody.freezeRotation = true;
originalRotation = transform.localRotation;
}
static function ClampAngle (angle : float, min : float, max : float) : float {
if (angle < -360.0)
angle += 360.0;
if (angle > 360.0)
angle -= 360.0;
return Mathf.Clamp (angle, min, max);
}
Your answer
Follow this Question
Related Questions
Look rotation is "Snappy" 1 Answer
Mouse look Y axis sensitivity doesn't change 0 Answers
Mouse sensitivity 2 Answers
How to make a mouse sensitivity slider? 1 Answer
Mouse Sensivty Problem 2 Answers