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

Clamping Rotation to be no greater or less than specified max/minium values?

Discussion in 'Scripting' started by Amon, Oct 29, 2011.

  1. Amon

    Amon

    Joined:
    Oct 18, 2009
    Posts:
    1,384
    I have a gun that sits in front of the camera. Think of it like a FPS Camera but not using standard FPS Player/Camera movement.

    Basically the mouse is used to move the gun, Camera stays fixed in Position/Rotation etc. The mouse only rotates the gun on the x,y axis.

    I need a way to clamp the values to basically not have the gun going round in circles. Say for example, rotate left/right buy a maximum value of 20 degrees then no matter how the mouse is moved it will go no further until the mouse is moved the opposite direction.

    Now I don't want people to think that I haven't tried or searched, I did; I found searches related to using Mathf.Clamp, it worked nearly in that it rotated the gun but it seemed to want to jump back to a middle value between the max/min of what was used in the Clamp command.

    I also found out that, according to the person who posted, it was better to work directly with the rotation x,y values by using transform.rotation.x or y etc. but I could never get anything to work.

    After two days of near triumph tests to complete fails I come to thee for help.

    If anybody can offer assistance I would greatly appreciate it.

    Thank You!
     
  2. Amon

    Amon

    Joined:
    Oct 18, 2009
    Posts:
    1,384
    Ok! May be if I asked it in a simpler way it could help.

    How do I Clamp Rotation on the x axis to be between say 90.0 and -90.0?
     
  3. justinlloyd

    justinlloyd

    Joined:
    Aug 5, 2010
    Posts:
    1,680
    How about a simple if test?
     
  4. Amon

    Amon

    Joined:
    Oct 18, 2009
    Posts:
    1,384
    I tried, Justin, but couldn't get anything to work. I know it seems simple for some but I'm still trying to figure out the logic of how Unity goes about doing things.

    Oh! And before unity I had never delved in to languages in brackety brackety land. I'm from a Basic Programming background and some of the concepts behind successful programming with Unity are still new to me.

    Apologies if I appear foolish for asking the question if it is something easy that all newcomers to Unity should be able to do.
     
  5. Amon

    Amon

    Joined:
    Oct 18, 2009
    Posts:
    1,384
    Ok! I have made further attempts. I've realised that when using GetAxis("Mouse X") it may not be returning values that transform.rotation.y can understand thus when the clamp limits are reached it just continues to spin.

    When I assign rotationY to transform.rotation.y then below that assign the values of GetAxis("Mouse X") to rotationY is when it, no matter what limit, keeps spinning with the mouse movement.

    I've noticed that GetAxis returns values of -1..1 so what I may need to do is somehow convert that to the same values used by the Rotation system ( Degrees? )?

    Time to experiment, again! :)
     
  6. scarpelius

    scarpelius

    Joined:
    Aug 19, 2007
    Posts:
    966
    This is taken from MouseLook.cs script usually attached to the first person controller. It might work studying it.

    Code (csharp):
    1.  
    2. if (axes == RotationAxes.MouseXAndY)
    3.         {
    4.             float rotationX = transform.localEulerAngles.y + Input.GetAxis("Mouse X") * sensitivityX;
    5.  
    6.             rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
    7.             rotationY = Mathf.Clamp(rotationY, minimumY, maximumY);
    8.  
    9.             transform.localEulerAngles = new Vector3(-rotationY, rotationX, 0);
    10.         }
    11.  
     
  7. justinlloyd

    justinlloyd

    Joined:
    Aug 5, 2010
    Posts:
    1,680
    Ah, I thought you were already converting your input axis to your rotation values.

    Scarpelius has posted some relevant code I believe will move you to your goal. I am working my way through my third pint of cider so am certainly not capable of writing out the relevant code at this time. I can barely handle this stupid raid dungeon I am 10-boxing.
     
  8. Amon

    Amon

    Joined:
    Oct 18, 2009
    Posts:
    1,384
    Code (csharp):
    1.  
    2. private var gunAngles:Vector3;
    3. private var gunRotationSpeed:float = 50.0;
    4.  
    5.  
    6. function Update () {
    7.        
    8.     var mxInput = Input.GetAxisRaw("Mouse Y");
    9.     var myInput = Input.GetAxisRaw("Mouse X");
    10.        
    11.     if ( mxInput != 0    myInput != 0 ) {
    12.         gunAngles.x += gunRotationSpeed * Time.deltaTime * mxInput;
    13.         gunAngles.y += gunRotationSpeed * Time.deltaTime * myInput;
    14.        
    15.         gunAngles.x = Mathf.Clamp(gunAngles.x, -3, 3);
    16.         gunAngles.y = Mathf.Clamp(gunAngles.y, -25.0, 9.0);
    17.        
    18.         transform.eulerAngles.x = -gunAngles.x;
    19.         transform.eulerAngles.y = gunAngles.y;
    20.     }
    21.    
    22. }
    23.  
    This does exactly what I need. It was a little messy at first i.e. bloated code but with refinement.......yeah right, I just went to Unity Answers, typed Clamp in the search box, the first Question in the list, then followed the code posted by Peter G.

    Had to modify it obviously to accommodate my game and thus we have the code I posted.

    Just thought I'd make the post with the code for anyone searching the forums; if they don't or haven't been to UnityAnswers yet.

    Thanks!
     
    Last edited: Oct 31, 2011
  9. justinlloyd

    justinlloyd

    Joined:
    Aug 5, 2010
    Posts:
    1,680
    Thanks for the update. Glad you figured it out. This will indeed be useful to future searchers.
     
  10. UnityFan18

    UnityFan18

    Joined:
    Jul 4, 2016
    Posts:
    62
    Hello, thank you for asking this question. I started learning Unity in 2016 and I have been working and researching this exact problem for over a month now. I don't come from a programming background, so a lot scripting is quite difficult and time intensive for me. I will try your code and see if that method works for clamping the rotation of my camera.
     
  11. UnityFan18

    UnityFan18

    Joined:
    Jul 4, 2016
    Posts:
    62
    In addition, Did you make any refinements/opmitzations to this code since you posted it in 2011?