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

How to limit the angle of the camera?

Discussion in 'Scripting' started by Temka193, Nov 27, 2013.

  1. Temka193

    Temka193

    Joined:
    Jul 12, 2013
    Posts:
    6
    here is a third-person camera, her MouseOrbit script. I need to limit the angle of rotation of the camera along the axis X, with respect to the Target. Target - an object rigidbody.

    You can do this:

    add to script variables
    Code (csharp):
    1. public var xMinLimit : float = -120;
    2. public var xMaxLimit : float = 120;
    Add in Lateupdate
    Code (csharp):
    1. x = ClampAngle(x, xMinLimit, xMaxLimit);
    and slightly lower values ​​multiply rotation
    Code (csharp):
    1. var baseRotation : Quaternion = target.rotation;
    2. rotation = baseRotation * Quaternion.Euler (y, x, 0);
    Everything works, the rotation angle is limited as it is necessary. The problem is that the camera must not be attached to the object rigidbody. Luggage must not depend on the rotation Target. Need to angle limited, but the camera was free. I've tried a lot of things, but nothing happens. Please help. Thanks in advance for your help.
     
  2. joessu

    joessu

    Joined:
    Nov 16, 2010
    Posts:
    88
    Get the angle and then constrain it with interpolation.

    To get the angle for the camera, you get the forward vector and vector to target and then get
    The inverse cosine of their dot product. The. It's just a matter of setting the proper angle via interpolation,

    And then applying the angle via a lookAtRotation or something to the cameras transform.

    EDIT: what exactly are you asking???
     
    Last edited: Nov 27, 2013
  3. Temka193

    Temka193

    Joined:
    Jul 12, 2013
    Posts:
    6
    I understand very little English, so I find it hard to understand what you're trying to say and can not accurately explained to the fact that I need.
    I am attaching a small scene, which I was able to do. There you can see clearly what I want to do. Need to rotate the mouse and press keys Q and E to rotate the Target object.
    This works as expected, but incorrectly, the camera jerks.
     

    Attached Files:

  4. vargata

    vargata

    Joined:
    Nov 26, 2013
    Posts:
    120
    wrong
     
    Last edited: Nov 28, 2013
  5. Temka193

    Temka193

    Joined:
    Jul 12, 2013
    Posts:
    6
    more please
     
  6. vargata

    vargata

    Joined:
    Nov 26, 2013
    Posts:
    120
    sry, what I've posted there was wrong not your question :)

    use this as mouseorbit script, i marked the modifications. it works for me, hope it will also work for you:

    Code (csharp):
    1.  
    2. var xMinText : TextMesh;
    3. var xMaxText : TextMesh;
    4. var CameraText : TextMesh;
    5.  
    6. var target : Transform;
    7. var distance = 10.0;
    8.  
    9. var xSpeed = 5.0;
    10. var ySpeed = 2.4;
    11.  
    12. var yMinLimit = -20;
    13. var yMaxLimit = 80;
    14.  
    15. var xMinLimit = -100;
    16. var xMaxLimit = 100;
    17.  
    18. private var x = 0.0;
    19. private var y = 0.0;
    20.            
    21. private var clampminx;
    22. private var clampmaxx;
    23.  
    24. @script AddComponentMenu("Camera-Control/Mouse Orbit")
    25.  
    26. function Start () {
    27.     var angles = transform.eulerAngles;
    28.     x = angles.y;
    29.     y = angles.x;
    30.  
    31.     // Make the rigid body not change rotation
    32.     if (rigidbody)
    33.         rigidbody.freezeRotation = true;
    34. }
    35.  
    36. function LateUpdate () {
    37.     if (target) {
    38.         x += Input.GetAxis("Mouse X") * xSpeed;
    39.         y -= Input.GetAxis("Mouse Y") * ySpeed;
    40.        
    41.             clampminx = target.eulerAngles.y + xMinLimit; //clamp angle translated to world
    42.             clampmaxx = target.eulerAngles.y + xMaxLimit;
    43.            
    44.             var tempx=ClampAngle(x, clampminx, clampmaxx); // test clamped angle
    45.            
    46.             if(x>tempx+100)x-=360; //if the difference is too big
    47.             else if(x<tempx-100)x+=360; //or too small, it means you have jumped a 360 degrees limit so inc or dec x with 360
    48.            
    49.             x = ClampAngle(x, clampminx, clampmaxx); //clamp the new x
    50.                        
    51.         y = ClampAngle(y, yMinLimit, yMaxLimit);
    52.  
    53.         //Debug
    54.         CameraText.text = x.ToString("f2");
    55.         Debug.DrawRay(target.position + Vector3(0,10,0), Quaternion.AngleAxis(xMinLimit + target.eulerAngles.y, target.up) * Vector3(0,0,-10), Color.red);
    56.         Debug.DrawRay(target.position + Vector3(0,10,0), Quaternion.AngleAxis(xMaxLimit + target.eulerAngles.y, target.up) * Vector3(0,0,-10), Color.red);
    57.         //Debug
    58.        
    59.         var rotation = Quaternion.Euler(y, x, 0);
    60.         var position = rotation * Vector3(0.0, 0.0, -distance) + target.position;
    61.        
    62.         transform.rotation = rotation;
    63.         transform.position = position;
    64.        
    65.         //Rotate target debug
    66.         if(Input.GetKey(KeyCode.Q))
    67.             target.Rotate(0, -1, 0);
    68.         if(Input.GetKey(KeyCode.E))
    69.             target.Rotate(0, 1, 0);
    70.         //Rotate target debug
    71.     }
    72. }
    73.  
    74. static function ClampAngle (angle : float, min : float, max : float) {
    75.     /*if (angle < -360) I've removed this as it would do double as I've done above and would make the camera jumping
    76.         angle += 360;
    77.     if (angle > 360)
    78.         angle -= 360;*/
    79.     return Mathf.Clamp (angle, min, max);
    80. }
    81.  
     
  7. Temka193

    Temka193

    Joined:
    Jul 12, 2013
    Posts:
    6
    Thank you so much, killed on that week to come up with an algorithm, you just saved me! :)