Search Unity

Mouselook when holding lmb or rmb problem

Discussion in 'Editor & General Support' started by krisian, Dec 7, 2013.

  1. krisian

    krisian

    Joined:
    Nov 22, 2013
    Posts:
    8
    Hello guys I am trying to create a script so that the mouselook only works when i hold the lmb or rmb. So far I can't make it to work . Thanks in advance!

    Here is the code

    using UnityEngine;
    using System.Collections;


    [AddComponentMenu("Camera-Control/Mouse Look Restricted")]
    public class MouseLookRestricted : MonoBehaviour {

    public enum RotationAxes { MouseXAndY = 0, MouseX = 1, MouseY = 2 }
    public RotationAxes axes = RotationAxes.MouseXAndY;
    public float sensitivityX = 15F;
    public float sensitivityY = 15F;

    public float minimumX = -360F;
    public float maximumX = 360F;

    public float minimumY = -60F;
    public float maximumY = 60F;

    float rotationY = 0F;

    void Update ()
    {
    //only do mouse look if right mouse button is down
    if (Input.GetMouseButton (1)|| Input.GetKey ("left shift") || Input.GetKey ("right shift")
    || Input.GetButton ("Horizontal") || Input.GetButton ("Vertical"))

    {
    transform.localEulerAngles = new Vector3 (-rotationY, transform.localEulerAngles.y, 0);
    }





    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);
    }
    }

    void Start ()
    {
    // Make the rigid body not change rotation
    if (rigidbody)
    rigidbody.freezeRotation = true;
    }
    }