Search Unity

How to clamp the camera rotation

Discussion in 'Scripting' started by bbQsauce, Sep 2, 2014.

  1. bbQsauce

    bbQsauce

    Joined:
    Jun 29, 2014
    Posts:
    53
    I would like to know how do i clamp the camera rotation in this situation.
    I've tried so many combinations but i'm stuck.. can't get it figured out..

    Basically, i have a touch zone that return me a drag delta ( as a Vector2D ) whenever i swipe in the zone.
    I just add that drag delta to the camera angle every frame to have a mouse look style script.
    But how i clamp the pitch between -30 and 30 for example?

    Here's the my current code in the Update function
    Code (CSharp):
    1.  
    2. void Update(){
    3.  
    4. // add the y delta to the camera ( up and down rotation )
    5. playerCamera.transform.eulerAngles += new Vector3 (delta.y, 0.0f, 0.0f);
    6.  
    7. // add the x delta to the PLAYER ( empty object with camera as child )
    8. transform.eulerAngles += new Vector3 (0.0f, delta.x, 0.0f);
    9.  
    10. }
    11.  
    I have already tried something like this... it clamp the vertical, but there's no point if i can't look horizontal anymore..
    Code (CSharp):
    1.  
    2. void Update(){
    3. pitch+=delta.y
    4. pitch=Mathf.Clamp(pitch,-30,30);
    5.  
    6. // set this every frame, so you can only look forward once the scene start                            
    7. playerCamera.transform.eulerAngles = new Vector3 (pitch, 0.0f, 0.0f);
    8.  
    9. // this overlaps with the line above
    10. transform.eulerAngles += new Vector3 (0.0f, deltaCm.x , 0.0f);
    11.  
    12. }
    13.  
    14.  
    Thanks in advance
     
    Last edited: Sep 2, 2014
  2. brianasu

    brianasu

    Joined:
    Mar 9, 2010
    Posts:
    369
    I think the built in mouseorbit.js has code to clamp rotation
     
  3. elmar1028

    elmar1028

    Joined:
    Nov 21, 2013
    Posts:
    2,359
    "Why not bother looking it up in the Google. I am sure another duplicate question will be fine!"

    Dude, I literally answered this question yesterday! *Sigh*

    Use the Mathf.Clamp. You can find out how to use it in the documentation.
     
  4. ThermalFusion

    ThermalFusion

    Joined:
    May 1, 2011
    Posts:
    906
    The trick is to keep a reference for both pitch and yaw externally.
    Code (csharp):
    1.  
    2. private void Update() {
    3.     pitch += delta.y;
    4.     pitch=Mathf.Clamp(pitch,-30f,30f);
    5.     yaw += delta.x;
    6.     yaw = Mathf.Repeat(yaw, 360f); // Keep between 0 and 360 if you want.
    7.  
    8.     playerCamera.transform.eulerAngles = new Vector3 (pitch, yaw, 0.0f);
    9. }
    10.  
     
  5. bbQsauce

    bbQsauce

    Joined:
    Jun 29, 2014
    Posts:
    53
    "Why should i read all through the post, i am sure this is a duplicated question".
    I don't seem to find a way where Mathf.Clamp should work, if you could read all the post, you will have noticed.


    That would work if i had only one object ( the main camera ) to rotate. Since i have a camera for pitch and an empty for yaw, this would not work, as adding another line such as
    playerEmptyObject.transform.eulerAngles= new Vector3(0.0f,yaw,0.0f);
    get in conflict with the player camera rotation.
     
  6. Fraconte

    Fraconte

    Joined:
    Dec 6, 2013
    Posts:
    327
    Just ignore yaw in the playerCamera and use it in the empty one.
     
  7. bbQsauce

    bbQsauce

    Joined:
    Jun 29, 2014
    Posts:
    53
    As i said, someway it gets in conflict with the camera pitch.. it gets shaky and buggy.
    By the way, i removed the empty object for now and applied all directly to the camera..