Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

[SOLVED] Limit Mouse Look View

Discussion in 'Scripting' started by HBK_, Jan 11, 2018.

  1. HBK_

    HBK_

    Joined:
    Dec 1, 2017
    Posts:
    87
    Hi everyone, I have this SimpleSmoothMouseLook script and i wanna limit it to a specific area like mouse look should only work on half screen not everywhere is it possible and how? Here's the script:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. [AddComponentMenu("Camera/Simple Smooth Mouse Look ")]
    5. public class SimpleSmoothMouseLook : MonoBehaviour
    6. {
    7.     Vector2 _mouseAbsolute;
    8.     Vector2 _smoothMouse;
    9.    
    10.     public Vector2 clampInDegrees = new Vector2(360, 180);
    11.     public bool lockCursor;
    12.     public Vector2 sensitivity = new Vector2(2, 2);
    13.     public Vector2 smoothing = new Vector2(3, 3);
    14.     public Vector2 targetDirection;
    15.     public Vector2 targetCharacterDirection;
    16.    
    17.     // Assign this if there's a parent object controlling motion, such as a Character Controller.
    18.     // Yaw rotation will affect this object instead of the camera if set.
    19.     public GameObject characterBody;
    20.    
    21.     void Start()
    22.     {
    23.         // Set target direction to the camera's initial orientation.
    24.         targetDirection = transform.localRotation.eulerAngles;
    25.        
    26.         // Set target direction for the character body to its inital state.
    27.         if (characterBody) targetCharacterDirection = characterBody.transform.localRotation.eulerAngles;
    28.     }
    29.    
    30.     void Update()
    31.     {
    32.         // Ensure the cursor is always locked when set
    33.         Screen.lockCursor = lockCursor;
    34.        
    35.         // Allow the script to clamp based on a desired target value.
    36.         var targetOrientation = Quaternion.Euler(targetDirection);
    37.         var targetCharacterOrientation = Quaternion.Euler(targetCharacterDirection);
    38.        
    39.         // Get raw mouse input for a cleaner reading on more sensitive mice.
    40.         var mouseDelta = new Vector2(Input.GetAxisRaw("Mouse X"), Input.GetAxisRaw("Mouse Y"));
    41.        
    42.         // Scale input against the sensitivity setting and multiply that against the smoothing value.
    43.         mouseDelta = Vector2.Scale(mouseDelta, new Vector2(sensitivity.x * smoothing.x, sensitivity.y * smoothing.y));
    44.        
    45.         // Interpolate mouse movement over time to apply smoothing delta.
    46.         _smoothMouse.x = Mathf.Lerp(_smoothMouse.x, mouseDelta.x, 1f / smoothing.x);
    47.         _smoothMouse.y = Mathf.Lerp(_smoothMouse.y, mouseDelta.y, 1f / smoothing.y);
    48.        
    49.         // Find the absolute mouse movement value from point zero.
    50.         _mouseAbsolute += _smoothMouse;
    51.        
    52.         // Clamp and apply the local x value first, so as not to be affected by world transforms.
    53.         if (clampInDegrees.x < 360)
    54.             _mouseAbsolute.x = Mathf.Clamp(_mouseAbsolute.x, -clampInDegrees.x * 0.5f, clampInDegrees.x * 0.5f);
    55.        
    56.         var xRotation = Quaternion.AngleAxis(-_mouseAbsolute.y, targetOrientation * Vector3.right);
    57.         transform.localRotation = xRotation;
    58.        
    59.         // Then clamp and apply the global y value.
    60.         if (clampInDegrees.y < 360)
    61.             _mouseAbsolute.y = Mathf.Clamp(_mouseAbsolute.y, -clampInDegrees.y * 0.5f, clampInDegrees.y * 0.5f);
    62.        
    63.         transform.localRotation *= targetOrientation;
    64.        
    65.         // If there's a character body that acts as a parent to the camera
    66.         if (characterBody)
    67.         {
    68.             var yRotation = Quaternion.AngleAxis(_mouseAbsolute.x, characterBody.transform.up);
    69.             characterBody.transform.localRotation = yRotation;
    70.             characterBody.transform.localRotation *= targetCharacterOrientation;
    71.         }
    72.         else
    73.         {
    74.             var yRotation = Quaternion.AngleAxis(_mouseAbsolute.x, transform.InverseTransformDirection(Vector3.up));
    75.             transform.localRotation *= yRotation;
    76.         }
    77.     }
    78. }

    Any Help would be highly Appreciated...
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,140
    You already have a spot where I see you are clamping. So you just need to clamp what value you can lerp to.
     
  3. HBK_

    HBK_

    Joined:
    Dec 1, 2017
    Posts:
    87
    Thank u so much for the info..i'm kinda noob in C# but i understand little so can you tell me how can i clamp values so that mouse look script should only work on half screen or something? it'll be really helpful!
     
  4. HBK_

    HBK_

    Joined:
    Dec 1, 2017
    Posts:
    87
    Thanks but your method was to clamp the radius/view i see so my player won't look/rotate up/down or left/right after reaching the decided clamp value but that's not i wanted.....BTW Thanks and I figured it out and customized it how i wanted...i am making a mobile fps game so i needed something to look around and this was working perfectly with touch so instead of limiting mouse view's area i got the code which was in update method and placed it in Repeat OnGUI Button and scaled it to the area i wanted the mouse look to work like half screen so on touching below that area the mouse look doesn't work and my player doesn't go crazy.....anyways i think i puzzled everyone....here's the script Hope it'll help someone else too.
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. [AddComponentMenu("Camera/Simple Smooth Mouse Look ")]
    5. public class SimpleSmoothMouseLook : MonoBehaviour
    6. {
    7.     Vector2 _mouseAbsolute;
    8.     Vector2 _smoothMouse;
    9.    
    10.     public Vector2 clampInDegrees = new Vector2(360, 180);
    11.     public bool lockCursor;
    12.     public Vector2 sensitivity = new Vector2(2, 2);
    13.     public Vector2 smoothing = new Vector2(3, 3);
    14.     public Vector2 targetDirection;
    15.     public Vector2 targetCharacterDirection;
    16.  
    17.     public float minimumX = -90F;
    18.     public float maximumX = 90F;
    19.     public float minimumY = -60F;
    20.     public float maximumY = 60F;
    21.  
    22.     // Assign this if there's a parent object controlling motion, such as a Character Controller.
    23.     // Yaw rotation will affect this object instead of the camera if set.
    24.     public GameObject characterBody;
    25.    
    26.     void Start()
    27.     {
    28.         // Set target direction to the camera's initial orientation.
    29.         targetDirection = transform.localRotation.eulerAngles;
    30.        
    31.         // Set target direction for the character body to its inital state.
    32.         if (characterBody) targetCharacterDirection = characterBody.transform.localRotation.eulerAngles;
    33.     }
    34.    
    35.      void OnGUI()
    36.     {//just for the sake of testing i made the button small so i can see what is going on.
    37.         if (GUI.RepeatButton (new Rect (580, 580, 580, 590), "move")){
    38.        
    39.         // Allow the script to clamp based on a desired target value.
    40.         var targetOrientation = Quaternion.Euler(targetDirection);
    41.         var targetCharacterOrientation = Quaternion.Euler(targetCharacterDirection);
    42.        
    43.         // Get raw mouse input for a cleaner reading on more sensitive mice.
    44.         var mouseDelta = new Vector2(Input.GetAxisRaw("Mouse X"), Input.GetAxisRaw("Mouse Y"));
    45.        
    46.         // Scale input against the sensitivity setting and multiply that against the smoothing value.
    47.         mouseDelta = Vector2.Scale(mouseDelta, new Vector2(sensitivity.x * smoothing.x, sensitivity.y * smoothing.y));
    48.        
    49.         // Interpolate mouse movement over time to apply smoothing delta.
    50.         _smoothMouse.x = Mathf.Lerp(_smoothMouse.x, mouseDelta.x, 1f / smoothing.x);
    51.         _smoothMouse.y = Mathf.Lerp(_smoothMouse.y, mouseDelta.y, 1f / smoothing.y);
    52.        
    53.         // Find the absolute mouse movement value from point zero.
    54.         _mouseAbsolute += _smoothMouse;
    55.        
    56.         // Clamp and apply the local x value first, so as not to be affected by world transforms.
    57.         if (clampInDegrees.x < 360)
    58.             _mouseAbsolute.x = Mathf.Clamp(_mouseAbsolute.x, minimumX, maximumX);
    59.        
    60.         var xRotation = Quaternion.AngleAxis(-_mouseAbsolute.y, targetOrientation * Vector3.right);
    61.         transform.localRotation = xRotation;
    62.        
    63.         // Then clamp and apply the global y value.
    64.         if (clampInDegrees.y < 360)
    65.             _mouseAbsolute.y = Mathf.Clamp(_mouseAbsolute.y, minimumY, maximumY);
    66.        
    67.         transform.localRotation *= targetOrientation;
    68.        
    69.         // If there's a character body that acts as a parent to the camera
    70.         if (characterBody)
    71.         {
    72.             var yRotation = Quaternion.AngleAxis(_mouseAbsolute.x, characterBody.transform.up);
    73.             characterBody.transform.localRotation = yRotation;
    74.             characterBody.transform.localRotation *= targetCharacterOrientation;
    75.         }
    76.         else
    77.         {
    78.             var yRotation = Quaternion.AngleAxis(_mouseAbsolute.x, transform.InverseTransformDirection(Vector3.up));
    79.             transform.localRotation *= yRotation;
    80.         }
    81.     }
    82. }
    83. }
     
    Last edited: Jan 12, 2018