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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

[SOLVED] Mouse look limit issue

Discussion in 'Scripting' started by alperkicirli, Apr 15, 2020.

  1. alperkicirli

    alperkicirli

    Joined:
    Apr 15, 2020
    Posts:
    10
    Hi everyone,
    The limits are not works. How can fix this?



    Code (CSharp):
    1. public class MouseLook : MonoBehaviour
    2.     {
    3.         public float speedH = 2.0f;
    4.         public float speedV = 2.0f;
    5.  
    6.         public float minimumX = -90F;
    7.         public float maximumX = 90F;
    8.         public float minimumY = -90F;
    9.         public float maximumY = 90F;
    10.  
    11.         private float yaw = 0.0f;
    12.         private float pitch = 0.0f;
    13.  
    14.         // Start is called before the first frame update
    15.         void Start()
    16.         {
    17.             Cursor.lockState = CursorLockMode.Locked;
    18.         }
    19.  
    20.         // Update is called once per frame
    21.         void Update()
    22.         {
    23.             yaw += speedH * Input.GetAxis("Mouse X");
    24.             yaw = Mathf.Clamp (yaw, minimumX, maximumX);
    25.             pitch -= speedV * Input.GetAxis("Mouse Y");
    26.             pitch = Mathf.Clamp(pitch, minimumY, maximumY);
    27.        
    28.             transform.eulerAngles = new Vector3(pitch, yaw, 0.0f);
    29.         }
    30.     }
     
    Last edited: Apr 15, 2020
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,735
    Can you describe the problem a bit more? Specifically:
    • What is the effect you are trying to accomplish with this script
    • What is the behavior you are actually seeing
    The more specific your descriptions, the better people will be able to help you.
     
  3. alperkicirli

    alperkicirli

    Joined:
    Apr 15, 2020
    Posts:
    10
    I'm trying to limit the looking around with the mouse. But nothing happens.
     
  4. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    "But nothing happens" is the opposite of specific. Certainly something is happening, and that something is not what you wanted, but it is hard to offer help without knowing what that something is.

    Try adding Debug.Log statements outputting all the variables in your Update. Look for any values which are unexpected. Try setting the limits in the inspector to values other than your current defaults and see if the behavior changes, for example you may want the limits set to 89 degrees instead of 90 degrees since over 90 degrees in one direction is less than 90 degrees in another so you may not actually be hitting your limit with those settings. Just debug, debug, debug and change things until you understand what is actually happening.
     
  5. alperkicirli

    alperkicirli

    Joined:
    Apr 15, 2020
    Posts:
    10
    Im solved. I changed value on scripts for every time. But this values are saving on the main camera. I'm forgot main camera.
    Sorry for my English. :)
     
    Joe-Censored likes this.