Search Unity

Restrict camera's rotation

Discussion in 'Scripting' started by balu89, Apr 17, 2008.

  1. balu89

    balu89

    Joined:
    Apr 15, 2008
    Posts:
    30
    Hi,

    I was wondering how could I restrict the rotation of my camera if a certain degree is met (let say <10 and >-10). By the way, I'm using a transform.LookAt(target);

    Thanks. :wink:
     
  2. Buff

    Buff

    Joined:
    Mar 21, 2008
    Posts:
    131
    You could try using the transform.eulerAngles inside an "if", but I'm not sure how it will work with the LookAt(target); :?
     
  3. balu89

    balu89

    Joined:
    Apr 15, 2008
    Posts:
    30
    Thanks but I don't know how to use that, I already tried without results.
    I will attach my script, I hope you have some ideas for my problem...

    Code (csharp):
    1.  
    2. var target : Transform;
    3.  
    4. function Update() {
    5.     transform.LookAt(target);
    6.     if (transform.rotation.y > 10) {
    7.     // Here I need something to restrict camera's rotation... But I Don't know how!!
    8.    
    9.     }
    10. }
    11.  
    12.  
    some idea????
     
  4. yellowlabrador

    yellowlabrador

    Joined:
    Oct 20, 2005
    Posts:
    562
    Not exactly sure how you are using it (lookAt) but the sentry gun script on the FPS tut and the mouse look script under component camera control script used some limits on the rotations.

    Something about clamping the AngleAxis. Take a look at the mouse look script it should help you in some way.

    Code (csharp):
    1.  
    2. var miny : float = -10;
    3. var maxy : float = 10;
    4.  
    5. function Update
    6. {
    7. transform.rotation = Quaternion.AngleAxis(miny, Vector3.left);
    8. }
    9.  
    Untested script but I think something like that. Maybe someone here will correct me if I'm thinking it wrong.

    Ray
     
  5. balu89

    balu89

    Joined:
    Apr 15, 2008
    Posts:
    30
    No luck with this either!

    This post is similar to my other post (Variable Question), but I'm trying to use another camera-script.

    The aim of this script is to have a static camera, which will follow a moving object. (thats why I use LoofAt). But I want to restrict the rotation of the camera (that is: when my gameObject gets to a specific position in Y, to make the camera restrict its X rotation). ---> The purpose of this is not to show off-screen game area.

    Please advice!

    Thanks. :roll:
     
  6. yellowlabrador

    yellowlabrador

    Joined:
    Oct 20, 2005
    Posts:
    562
    from the docs.


    Code (csharp):
    1.  
    2. //Quaternion.eulerAngles
    3.  
    4. var eulerAngles : Vector3
    5.  
    6. //Description Returns the euler angle representation of the rotation.
    7. //A rotation that rotates euler.x degrees around the x axis. euler.y //degrees around the y axis. euler.z degrees around the z axis.
    8.  
    9. // Create a rotation
    10. var rotation = Quaternion.identity;
    11. // Assign a rotation 30 degrees around the y axis
    12. rotation.eulerAngles = Vector3(0, 30, 0);
    13. // print the rotation around the y-axis
    14. print(rotation.eulerAngles.y);
    15.  
    16.  
     
  7. yellowlabrador

    yellowlabrador

    Joined:
    Oct 20, 2005
    Posts:
    562
    this thing is bugging me so I was playing with it and what I did is exposed the camera euleraAngles in the inspector
    Code (csharp):
    1.  
    2. var target : Transform;
    3. var myCam : Camera;
    4.  
    5. var miny = 0.0;
    6. function Update ()
    7. {
    8.     miny = myCam.transform.eulerAngles.y;
    9.  
    10.     var relativePos = target.position - transform.position;
    11.     var rotation = Quaternion.LookRotation(relativePos);
    12.     transform.rotation = rotation;
    13. }
    14.  
    15.  
    will play some more later, had a feeling I will need such script later :D

    Hope it helps,

    Ray
     
  8. balu89

    balu89

    Joined:
    Apr 15, 2008
    Posts:
    30
    Hi,

    I tried your script. Thank you very much for your help, but still its not working the way I want it to.

    I set all the variables in the inspector as they should be, and set the minx (I need x-axis restriction) to -10.

    When I hit play, the camera works just like a lookAt(), but keeps following my object even when it crosses the -10 degrees. How can I make it to stop at -10?

    Thanks :?
     
  9. balu89

    balu89

    Joined:
    Apr 15, 2008
    Posts:
    30
    Hello,

    This is what I'm trying to do in my scene. As I was saying, I want my camera to stop rotating in X when it reaches -10 (or +10 if it is following the object upwards...). How can I do that???

    Thank you! :D
     

    Attached Files:

  10. yellowlabrador

    yellowlabrador

    Joined:
    Oct 20, 2005
    Posts:
    562
    This migh not work exactly the way you need it but try playing with it


    Code (csharp):
    1. var target : Transform; //the object we want to look at
    2. var myCam : Camera; the camera we use to look at the object
    3. var minx = 0.0;
    4.  
    5.  
    6. function Update () {
    7.     //This is not working!
    8.      // this is only there so I can see what the numbers are in the inspector, it helps me lots of times.
    9. minx = myCam.transform.eulerAngles.x;
    10.  
    11.     var relativePos = target.position - transform.position;
    12.     var rotation = Quaternion.LookRotation(relativePos);
    13.     transform.rotation = rotation;
    14.    
    15. //if condition you need to change depending on the position of the target(limits that you want the cam to stop looking)
    16.  
    17. //Since your cube is just dropping from top to bottom, I used 15.0 as my limit, so when target position reach 15.0 cam rotation is set to 10 in the X
    18. // I use Vector3.left but the doc says Vector3.right, so not sure if that's okay, works ok,  can someone please correct me here). :D
    19.  
    20.  if(target.transform.position.y <= 15.0)
    21.     {
    22.         transform.rotation = Quaternion.AngleAxis(10,Vector3.left);
    23.         print("I got called"); // helps to find out if this code is getting called;
    24.     }
    25. }
    take a look at Vector3. Angle lso in the docs

    Ray
     
  11. Charles Hinshaw

    Charles Hinshaw

    Joined:
    Feb 6, 2008
    Posts:
    1,070
    From what I gather, constraining quaternions along an axis isn't fun. Much easier to get Euler angles from the quaternion, constrain them and then build a new quaternion for rotation.

    just have to remember that something Quaternion.eulerAngle.y is going to be rotation around the Y axis, not visual Y moving up and down. Also that you won't be getting values like -10 for that, all of them will be from 0 to 360.

    This works. It is from a longer script that I had that was free mouse look unless a target was assigned and then it looked at the target. I needed constraints to match the constraints on the free mouse look, so some of this isn't well structured for this exact task, but I just pasted it into your example file, and it tracks the object but constrains camera rotation.

    note that this doesn't take into account the original rotation for the camera, if any

    Code (csharp):
    1.  
    2. var target : Transform;
    3. var damping = 5.0;
    4. var xConstraint = 360;
    5. var yConstraint = 30;
    6.  
    7. function Update () {
    8.     if (target) {
    9.         var rot = Quaternion.LookRotation(target.position - transform.position);
    10.         var rotationX = rot.eulerAngles.y;
    11.         var rotationY = rot.eulerAngles.x;
    12.         rotationX = ClampAngle(rotationX, -xConstraint, xConstraint);
    13.         rotationY = ClampAngle(rotationY, -yConstraint, yConstraint);
    14.         rotationY = -rotationY;
    15.         var xQuaternion = Quaternion.AngleAxis(rotationX, Vector3.up);
    16.         var yQuaternion = Quaternion.AngleAxis(rotationY, Vector3.left);
    17.         transform.localRotation = Quaternion.Slerp(transform.localRotation, xQuaternion * yQuaternion, Time.deltaTime * damping);
    18.     }
    19. }
    20.  
    21.  
    22. function ClampAngle(angle, min, max){
    23.     if (target){
    24.         if (min < 0) min += 360;
    25.         if (angle > 180){
    26.             return Mathf.Clamp(angle, min, 360);       
    27.         } else {
    28.             return Mathf.Clamp(angle, 0, max);
    29.         }
    30.     }
    31. }
     
  12. balu89

    balu89

    Joined:
    Apr 15, 2008
    Posts:
    30
    Thank you guys for your help!
    CharlesHinshaw!!! Thats exactly what I needed!!! Thanks for sharing your knowledge! :D
     
  13. shamalic

    shamalic

    Joined:
    Aug 19, 2015
    Posts:
    5
    I am facing the same problem but the script didn't work for me... It keeps saying that

    (25,26): BCE0051: Operator '+' cannot be used with a left hand side of type 'Object' and a right hand side of type 'int'
    (25,19): BCE0051: Operator '>' cannot be used with a left hand side of type 'Object' and a right hand side of type 'int'
    (25,17): BCE0051: Operator '<' cannot be used with a left hand side of type 'Object' and a right hand side of type 'int'

    How to fix this?
     
  14. shamalic

    shamalic

    Joined:
    Aug 19, 2015
    Posts:
    5
    I solved it by writing like this

    function ClampAngle(angle: float, min: float, max: float){
     
  15. Cladri

    Cladri

    Joined:
    Aug 18, 2017
    Posts:
    1
    An easy way to do this in C# is using the Range atribute,I don't know if that'll work in JS tho,try looking for something that works that way in JS.