- Home /
Mouse look on right click
Hi I'm trying to add the functionality to operate mouse look only when the right mouse button is held down. I understand I have to wrap the update function in this code
if(Input.GetButton ("Fire1")){
//Current code goes here...
}
however I can't seem to get it to work. Would it be possible for someone to show me exactly how to implement the above code into the mouselook script.
Thanks
Answer by Maarten · Nov 24, 2010 at 10:36 AM
Change the Update method of MouseLook.cs to the following:
void Update () { if (Input.GetButton("Fire1")) { if (axes == RotationAxes.MouseXAndY) { float rotationX = transform.localEulerAngles.y + Input.GetAxis("Mouse X") * sensitivityX;
rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
rotationY = Mathf.Clamp(rotationY, minimumY, maximumY);
transform.localEulerAngles = new Vector3(-rotationY, rotationX, 0);
}
else if (axes == RotationAxes.MouseX)
{
transform.Rotate(0, Input.GetAxis("Mouse X") * sensitivityX, 0);
}
else
{
rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
rotationY = Mathf.Clamp(rotationY, minimumY, maximumY);
transform.localEulerAngles = new Vector3(-rotationY, transform.localEulerAngles.y, 0);
}
}
}
Instead of using Input.GetButton("Fire1"), you can use Input.GetMouseButton(1), which represents the right mouse button.
Excellent, that works perfectly. I'm still not sure what I was doing wrong, I think I'm struggling with placement of { }.
Anyway, Thanks for the great help.