Search Unity

  1. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice
  2. Ever participated in one our Game Jams? Want pointers on your project? Our Evangelists will be available on Friday to give feedback. Come share your games with us!
    Dismiss Notice

Mouselook when holding lmb or rmb problem

Discussion in 'Scripting' 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;
    }
    }
     
  2. agentsmith

    agentsmith

    Joined:
    May 1, 2010
    Posts:
    132
    One way to do it is to have your if statement brackets encapsulate the rest of the code in the Update function:

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5.  
    6. [AddComponentMenu ( "Camera-Control/Mouse Look Restricted" )]
    7. public class MouseLookRestricted : MonoBehaviour
    8. {
    9.  
    10.     public enum RotationAxes { MouseXAndY = 0, MouseX = 1, MouseY = 2 }
    11.     public RotationAxes axes = RotationAxes.MouseXAndY;
    12.     public float sensitivityX = 15F;
    13.     public float sensitivityY = 15F;
    14.  
    15.     public float minimumX = -360F;
    16.     public float maximumX = 360F;
    17.  
    18.     public float minimumY = -60F;
    19.     public float maximumY = 60F;
    20.  
    21.     float rotationY = 0F;
    22.  
    23.     void Update ( )
    24.     {
    25.         //only do mouse look if right mouse button is down
    26.         if ( Input.GetMouseButton ( 0 ) || Input.GetMouseButton ( 1 ) )
    27.         {
    28.             transform.localEulerAngles = new Vector3 ( -rotationY, transform.localEulerAngles.y, 0 );
    29.  
    30.             if ( axes == RotationAxes.MouseXAndY )
    31.             {
    32.                 float rotationX = transform.localEulerAngles.y + Input.GetAxis ( "Mouse X" ) * sensitivityX;
    33.  
    34.                 rotationY += Input.GetAxis ( "Mouse Y" ) * sensitivityY;
    35.                 rotationY = Mathf.Clamp ( rotationY, minimumY, maximumY );
    36.  
    37.                 transform.localEulerAngles = new Vector3 ( -rotationY, rotationX, 0 );
    38.             }
    39.             else if ( axes == RotationAxes.MouseX )
    40.             {
    41.                 transform.Rotate ( 0, Input.GetAxis ( "Mouse X" ) * sensitivityX, 0 );
    42.             }
    43.             else
    44.             {
    45.                 rotationY += Input.GetAxis ( "Mouse Y" ) * sensitivityY;
    46.                 rotationY = Mathf.Clamp ( rotationY, minimumY, maximumY );
    47.  
    48.                 transform.localEulerAngles = new Vector3 ( -rotationY, transform.localEulerAngles.y, 0 );
    49.             }
    50.         }
    51.     }
    52.  
    53.     void Start ( )
    54.     {
    55.         // Make the rigid body not change rotation
    56.         if ( rigidbody )
    57.             rigidbody.freezeRotation = true;
    58.     }
    59. }
    60.  
    And here is what your original example would have been...

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5.  
    6. [AddComponentMenu ( "Camera-Control/Mouse Look Restricted" )]
    7. public class MouseLookRestricted : MonoBehaviour
    8. {
    9.  
    10.     public enum RotationAxes { MouseXAndY = 0, MouseX = 1, MouseY = 2 }
    11.     public RotationAxes axes = RotationAxes.MouseXAndY;
    12.     public float sensitivityX = 15F;
    13.     public float sensitivityY = 15F;
    14.  
    15.     public float minimumX = -360F;
    16.     public float maximumX = 360F;
    17.  
    18.     public float minimumY = -60F;
    19.     public float maximumY = 60F;
    20.  
    21.     float rotationY = 0F;
    22.  
    23.     void Update ( )
    24.     {
    25.         //only do mouse look if right mouse button is down
    26.         if ( Input.GetMouseButton ( 1 ) || Input.GetKey ( "left shift" ) || Input.GetKey ( "right shift" )
    27.         || Input.GetButton ( "Horizontal" ) || Input.GetButton ( "Vertical" ) )
    28.         {
    29.             transform.localEulerAngles = new Vector3 ( -rotationY, transform.localEulerAngles.y, 0 );
    30.  
    31.             if ( axes == RotationAxes.MouseXAndY )
    32.             {
    33.                 float rotationX = transform.localEulerAngles.y + Input.GetAxis ( "Mouse X" ) * sensitivityX;
    34.  
    35.                 rotationY += Input.GetAxis ( "Mouse Y" ) * sensitivityY;
    36.                 rotationY = Mathf.Clamp ( rotationY, minimumY, maximumY );
    37.  
    38.                 transform.localEulerAngles = new Vector3 ( -rotationY, rotationX, 0 );
    39.             }
    40.             else if ( axes == RotationAxes.MouseX )
    41.             {
    42.                 transform.Rotate ( 0, Input.GetAxis ( "Mouse X" ) * sensitivityX, 0 );
    43.             }
    44.             else
    45.             {
    46.                 rotationY += Input.GetAxis ( "Mouse Y" ) * sensitivityY;
    47.                 rotationY = Mathf.Clamp ( rotationY, minimumY, maximumY );
    48.  
    49.                 transform.localEulerAngles = new Vector3 ( -rotationY, transform.localEulerAngles.y, 0 );
    50.             }
    51.         }
    52.     }
    53.  
    54.     void Start ( )
    55.     {
    56.         // Make the rigid body not change rotation
    57.         if ( rigidbody )
    58.             rigidbody.freezeRotation = true;
    59.     }
    60. }
    61.  
     
  3. krisian

    krisian

    Joined:
    Nov 22, 2013
    Posts:
    8
    Thanks! Although when I tried it it still functions like a normal fps controller.What I am trying to achieve that you need to hold rmb so that I can look around when moving the mouse, and when not the mouse does not function to look.THanks!
     
  4. agentsmith

    agentsmith

    Joined:
    May 1, 2010
    Posts:
    132
    When I put the code I posted in a script an attach it to a camera it moves the camera only when the mouse button is pressed as requested.

    Hmm I'm a little unsure of what you're requesting... If you want a different "type" of camera, ie orbiting, or WoW-like camera, then you'll need to elaborate more. If there's an existing game with the controls or camera your attempting to replicate that'd help too.
     
  5. ForbiddenSoul

    ForbiddenSoul

    Joined:
    Oct 1, 2013
    Posts:
    1
    Yeah man I tried that .cs script and it works flawlessly.

    is what is really messing you up.

    That says if you press the right mouse button or left shift or right shift or press any of WASD or UP DOWN LEFT RIGHT the camera will move. did you possibly bind Horizontal and Vertical to the mouse axies? That will mean anytime you move the mouse the camera will move.

    Either way copy paste the previous script and It will work, or fix your if statement...
     
  6. krisian

    krisian

    Joined:
    Nov 22, 2013
    Posts:
    8
    I tried restarting unity and it works now! I dunno why it didn't work on the first time. Thank you so much!
     
unityunity