look sensitivity when aiming problem [SOLVED]

The idea is that when the gun is zoomed in the look sensitivity decreases and when you zoom out again it increases. I have made adjustments to my mouselook script but the sensitivity doesn’t change. What’s going on?

var lookSensitivity : float = 5;  //Look sensitivity
@HideInInspector  //Hides the variable in the inspector- So I only have what I need to see in the inspector
var yRotation : float;
@HideInInspector
var xRotation : float;
@HideInInspector
var currentYRotation : float;
@HideInInspector
var currentXRotation : float;
@HideInInspector
var yRotationV : float;
@HideInInspector
var xRotationV : float;
var currentAimRatio : float = 1;
var lookSmoothDamp : float = 0.1;
var headbobSpeed : float = 1;
var headbobStepCounter : float;
var headbobAmountX : float = 1;
var headbobAmountY : float = 1;
var parentLastPos : Vector3;
var eyeHeightRatio : float = 0.9;
var cameraMain_2 : boolean = false;


function Awake ()
{
    parentLastPos = transform.parent.position;
}

function Update ()
{   
    if (transform.parent.GetComponent(PlayerMovementScript).grounded)
        headbobStepCounter += Vector3.Distance(parentLastPos, transform.parent.position) * headbobSpeed;
    transform.localPosition.x = Mathf.Sin(headbobStepCounter) * headbobAmountX * currentAimRatio;
    transform.localPosition.y = (Mathf.Cos(headbobStepCounter * 2) * headbobAmountY * -1 * currentAimRatio) + (transform.parent.localScale.y * eyeHeightRatio) - (transform.parent.localScale.y /2);
   
    parentLastPos = transform.parent.position;
   
    if(Input.GetButtonDown("Fire2")
    {
         cameraMain_2 = true;
    }
   
    if ( cameraMain_2 == true)
    {
        yRotation += Input.GetAxis("Mouse X") * lookSensitivity - 5;
        xRotation += Input.GetAxis("Mouse Y") * lookSensitivity - 5;
    }
    else
    {
        yRotation += Input.GetAxis("Mouse X") * lookSensitivity;
        xRotation += Input.GetAxis("Mouse Y") * lookSensitivity;
    }

    xRotation = Mathf.Clamp(xRotation, -90, 90); //Prevents the camera from moving more than 90* vertically
   
    currentXRotation = Mathf.SmoothDamp(currentXRotation, xRotation, xRotationV, lookSmoothDamp);
    currentYRotation = Mathf.SmoothDamp(currentYRotation, yRotation, yRotationV, lookSmoothDamp);
   
    transform.rotation = Quaternion.Euler(currentXRotation, currentYRotation, 0);
}

//Use Debug.Log("example"); to print out text on the Unity Console- place at bottom of code, useful for checking that chuncks of code are working/ being utilized

You have an order of operations problem. Replace

yRotation += Input.GetAxis(“Mouse X”)* lookSensitivity -5;

with

yRotation += Input.GetAxis(“Mouse X”)* (lookSensitivity -5);

I suggest you DON’T remove the sensitivity by a flat amount rather a perscentage IE, when you zoomed in you are 50% slower etc. to achive this make a variable called ‘m_lookDecrease’ where m_lookDecrease is a value from 0.0->1.0

yRotation += Input.GetAxis(“Mouse X”)* (lookSensitivity * m_lookDecrease);

I added what you said and now it does slow down the sensitivity when aimed in but when I aim out again the sensitivity stays slow.

var lookSensitivity : float = 5;  //Look sensitivity
@HideInInspector  //Hides the variable in the inspector- So I only have what I need to see in the inspector
var yRotation : float;
@HideInInspector
var xRotation : float;
@HideInInspector
var currentYRotation : float;
@HideInInspector
var currentXRotation : float;
@HideInInspector
var yRotationV : float;
@HideInInspector
var xRotationV : float;
var currentAimRatio : float = 1;
var lookSmoothDamp : float = 0.1;
var headbobSpeed : float = 1;
var headbobStepCounter : float;
var headbobAmountX : float = 1;
var headbobAmountY : float = 1;
var parentLastPos : Vector3;
var eyeHeightRatio : float = 0.9;
var cameraMain_2 : boolean = false;
var m_lookDecrease : float = 0.5;
function Awake ()
{
    parentLastPos = transform.parent.position;
}
function Update ()
{ 
    if (transform.parent.GetComponent(PlayerMovementScript).grounded)
        headbobStepCounter += Vector3.Distance(parentLastPos, transform.parent.position) * headbobSpeed;
    transform.localPosition.x = Mathf.Sin(headbobStepCounter) * headbobAmountX * currentAimRatio;
    transform.localPosition.y = (Mathf.Cos(headbobStepCounter * 2) * headbobAmountY * -1 * currentAimRatio) + (transform.parent.localScale.y * eyeHeightRatio) - (transform.parent.localScale.y /2);
  
    parentLastPos = transform.parent.position;
  
    if(Input.GetButton("Fire2"))
    {
         cameraMain_2 = true;
    }
  
    if ( cameraMain_2 == true)
    {
        yRotation += Input.GetAxis("Mouse X")* (lookSensitivity * m_lookDecrease);
        xRotation += Input.GetAxis("Mouse Y") * (lookSensitivity * m_lookDecrease);
    }
   
    else
    {   
        cameraMain_2 = false;
        yRotation += Input.GetAxis("Mouse X") * lookSensitivity;
        xRotation += Input.GetAxis("Mouse Y") * lookSensitivity; 
    }
    xRotation = Mathf.Clamp(xRotation, -90, 90); //Prevents the camera from moving more than 90* vertically
  
    currentXRotation = Mathf.SmoothDamp(currentXRotation, xRotation, xRotationV, lookSmoothDamp);
    currentYRotation = Mathf.SmoothDamp(currentYRotation, yRotation, yRotationV, lookSmoothDamp);
  
    transform.rotation = Quaternion.Euler(currentXRotation, currentYRotation, 0);
}
//Use Debug.Log("example"); to print out text on the Unity Console- place at bottom of code, useful for checking that chuncks of code are working/ being utilized

Bump

where do you change cameraMain_2 to false except for when it is false?
try changing

if(Input.MouseButtonDown(1))
{
    cameraMain_2 = !cameraMain_2;
}

and remove the
cameraMain_2 = false; in your else. of course cameraMain_2 is false if it’s not true, unless you’r on a quantum computer :wink:

haha yeah I ended up changing the entire aiming in mechanism as it was buggy and unrealistic which also solved this problem so bonus :slight_smile: