Page 1 of 1

Mouse Look

Posted: Thu Sep 09, 2010 2:17 am
by SweetRevenge
I was wondering if I could add something to the Mouse Look script in unity so I can turn this function on and off, so like if I press shift then it will turn off the mouse look script and I can click on a gui that just popped up, and when i'm ready to start moving again, just press shift and keep going.

Re: Mouse Look

Posted: Mon Sep 27, 2010 2:27 pm
by Jediknight846
I am not a expert programmer, and I have no idea if this will work, and if it works then you would have to hold shift down to look, but here:

Code: Select all

    void Update ()
    {
        if(Input.GetButton("Shift"))
        {
        if (axes == RotationAxes.MouseXAndY)
        {
            // Read the mouse input axis
            rotationX += Input.GetAxis("Mouse X") * sensitivityX;
            rotationY += Input.GetAxis("Mouse Y") * sensitivityY;

            rotationX = ClampAngle (rotationX, minimumX, maximumX);
            rotationY = ClampAngle (rotationY, minimumY, maximumY);
            
            Quaternion xQuaternion = Quaternion.AngleAxis (rotationX, Vector3.up);
            Quaternion 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);

            Quaternion xQuaternion = Quaternion.AngleAxis (rotationX, Vector3.up);
            transform.localRotation = originalRotation * xQuaternion;
        }
        else
        {
            rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
            rotationY = ClampAngle (rotationY, minimumY, maximumY);

            Quaternion yQuaternion = Quaternion.AngleAxis (rotationY, Vector3.left);
            transform.localRotation = originalRotation * yQuaternion;
        }
    }
    }
replace that part of the code with this. Hope it helps.

Re: Mouse Look

Posted: Mon Sep 27, 2010 3:02 pm
by Noctrine
Jedi is in the right direction, but you'd want to set a variable. Remember, Update calls every few seconds, so that will only work if you are holding the button down. If you toggle a boolean you can have it set to on/off.