Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

constrain cameras movement on Y axis

Discussion in 'Scripting' started by Greg Farina, May 3, 2007.

  1. Greg Farina

    Greg Farina

    Joined:
    Apr 25, 2007
    Posts:
    47
    Hi guys,

    I'm trying to constrain the cameras movement so the user cannot go below 0 degrees on the Y axis. I basically want the user to only manipulate camera on the upper hemisphere of a sphere. How can I alter this to achieve that effect?

    Thanks,

    Greg





    Code (csharp):
    1. var target : Transform;
    2. var distance = 5.0;
    3. var xSensitivity = 5.0;
    4. var ySensitivity = 5.0;
    5. var xAngle = 0.0;
    6. var yAngle = 0.0;
    7.  
    8. function LateUpdate ()
    9. {
    10.     // Only if there is a target
    11.     if (target)
    12.     {
    13.         // Update x, y angle with the mouse delta
    14.         xAngle += Input.GetAxis ("Mouse X") * xSensitivity;
    15.         yAngle -= Input.GetAxis ("Mouse Y") * ySensitivity;
    16.  
    17.         // Initialize the position to be distance units along the z axis
    18.         // away from the target
    19.         transform.position = Vector3.fwd * distance + target.position;
    20.  
    21.         // Initialize the rotation to look at the target
    22.         transform.LookAt (target.position);
    23.        
    24.         // Rotate around the world up axis by the accumulated delta mouse x
    25.         transform.RotateAround (target.position, Vector3.up, xAngle);
    26.  
    27.         // Rotate around our own right vector by the accumulated delta mouse y
    28.         worldRight = transform.TransformDirection (Vector3.right);
    29.         transform.RotateAround (target.position, worldRight, yAngle);
    30.     }
    31. }
     
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Mathf.Clamp is always useful for constraining values. So after the "yAngle -= Input..." line, insert this:

    Code (csharp):
    1.  
    2.       yAngle = Mathf.Clamp(yAngle, 0, 180);
    3.  
    That way the angle of rotation will have to stay in the upper half of the sphere...it can't go below 0 or above 180.

    --Eric
     
  3. Greg Farina

    Greg Farina

    Joined:
    Apr 25, 2007
    Posts:
    47
    Wow, thanks, Eric! I really appreciate your time and help on this. As you can tell I am a complete novice and learning as I go.

    Thanks again,

    greg
     
  4. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    You have to start somewhere. :) Learning as you go is the way to do it...reading through all the docs and then jumping in and expecting to be able to do everything doesn't work at all...at least not for me. Of course, the docs are generally excellent reference and you should eventually read through them all as you go along. Probably more than once, since stuff that doesn't make sense at first will likely be understandable later when you have more experience. But especially at the beginning, you sometimes aren't even sure where to start looking. Fortunately OTEE has some nice forums. ;)

    --Eric
     
  5. Greg Farina

    Greg Farina

    Joined:
    Apr 25, 2007
    Posts:
    47
    I've been doing my best to try and get through the tutorials / examples, but it's still quite difficult to understand some scripting commands. Fortunately, I am understanding more and more thanks to your generosity! I hate to be a pest, but would you happen to know how to constrain camera movement to the right mouse button in the held down position?

    Thanks again!

    -Greg
     
  6. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    You can use GetMouseButton to check for mouse button presses. In this case you would only want to check for mouse movement if the right button is down, so wrap the GetMouseButton check around the input code, like so:

    Code (csharp):
    1.  
    2.       if (Input.GetMouseButton(1) ) {
    3.          xAngle += Input.GetAxis ("Mouse X") * xSensitivity;
    4.          yAngle -= Input.GetAxis ("Mouse Y") * ySensitivity;
    5.          yAngle = Mathf.Clamp(yAngle, 0, 180);
    6.       }
    7.  
    With GetMouseButton, you use 0 for left, 1 for right, and 2 for middle. That's actually shorthand there...it might be less confusing if I wrote

    Code (csharp):
    1.  
    2. if (Input.GetMouseButton(1) == true) {
    3.  
    Both methods mean the same thing and do the same thing.

    --Eric
     
  7. Greg Farina

    Greg Farina

    Joined:
    Apr 25, 2007
    Posts:
    47
    Thanks, Eric, once again I owe you, pal. I am very grateful!