Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question Mathf Clamp seems to have no effect

Discussion in 'Scripting' started by gjfnasjdnasklnd, Jun 15, 2020.

  1. gjfnasjdnasklnd

    gjfnasjdnasklnd

    Joined:
    Jun 15, 2020
    Posts:
    8
    Hi,

    This is for a first person player controller script.

    I have horizontal rotation setup like so on the player object transform:

    Code (CSharp):
    1.     private void rotateCameraHorizontal(float mouseInputX)
    2.     {
    3.         Quaternion rotation = transform.rotation; //get current rotation
    4.  
    5.         rotation = Quaternion.Euler(rotation.eulerAngles.x, rotation.eulerAngles.y + mouseInputX, rotation.eulerAngles.z);
    6.         transform.rotation = rotation; //set new rotation
    7.     }
    I have vertical rotation setup on the player camera:
    Code (CSharp):
    1. private void rotateCameraVertical(float mouseInputY)
    2.     {
    3.     float clampedYInput = Mathf.Clamp(mouseInputY, -90f, 90f); //this doesnt seem to affect anything.
    4.  
    5.     ObjectToApplyCameraTransform.rotation =     Quaternion.Euler(ObjectToApplyCameraTransform.rotation.eulerAngles + new Vector3(clampedYInput, 0f, 0f));
    6.     }
    The clamping does not seem to make a difference? Any advice?
     
  2. WarmedxMints

    WarmedxMints

    Joined:
    Feb 6, 2017
    Posts:
    1,035
    If mouseInputY is simply the input from the mouse axis, it is not going to get anywhere near 90 + or -. How is that value being calculated before being sent to the method?
     
  3. gjfnasjdnasklnd

    gjfnasjdnasklnd

    Joined:
    Jun 15, 2020
    Posts:
    8
    Edit: Yes, mouse input is just the input from the mouse axis. How should I be going about this? At the moment the mouse is limited to 90 degrees up and down but if you repeatedly force the mouse against either "north/south pole" the screen will go upside down.

    Code (CSharp):
    1.     private Vector2 getRawMouseInput()
    2.     {
    3.         Vector2 mouseInput = new Vector2(Input.GetAxisRaw("Mouse X"), Input.GetAxisRaw("Mouse Y")) * mouseSensitivity;
    4.  
    5.         mouseInput.y = invertY ? mouseInput.y : -mouseInput.y; //invert Y check
    6.  
    7.         return mouseInput;
    8.     }
    9.  
    10.     private void moveCamera()
    11.     {
    12.         Vector2 mouseInput = getRawMouseInput();
    13.         rotateCameraHorizontal(mouseInput.x);
    14.         rotateCameraVertical(mouseInput.y);
    15.     }
     
    Last edited: Jun 15, 2020
  4. WarmedxMints

    WarmedxMints

    Joined:
    Feb 6, 2017
    Posts:
    1,035
    put a Debug.Log(mouseInput.y) in your moveCamera method and see if it ever gets anywhere near 90
     
    gjfnasjdnasklnd likes this.
  5. gjfnasjdnasklnd

    gjfnasjdnasklnd

    Joined:
    Jun 15, 2020
    Posts:
    8
    Ah right, its simply input being added to the rotation. Hmmm. Can you advise on how I should go about this? I edited my previous comment with the upside down issue btw.

    Am I doing XY rotation properly then? Should I be rotating the camera and transform separately like this, it's just something I gathered from watching a bunch of FPS scripts and found Mathf.Clamp used by some scripts but I must have misunderstood them.