Search Unity

Creating boundaries

Discussion in 'Scripting' started by Luke-Houlihan, Oct 23, 2007.

  1. Luke-Houlihan

    Luke-Houlihan

    Joined:
    Jun 26, 2007
    Posts:
    303
    I am trying to script some boundaries for my camera object but I for the life of me cannot get it right. I tried using Bounds() but cant get the errors out. How should I go about doing this?
     
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Probably Mathf.Clamp() would be the simplest. Bounds is for reading/setting bounding box volumes for objects.

    --Eric
     
  3. Rick B

    Rick B

    Joined:
    Nov 1, 2006
    Posts:
    9
    Maybe I misundertsand what you are trying to do, but couldn't you attach some geometry to your camera, like the pre-fab first person controller, then create geometry that matches your boundaries and make that a mesh collider?
     
  4. Luke-Houlihan

    Luke-Houlihan

    Joined:
    Jun 26, 2007
    Posts:
    303
    I have a camera that moves in the x and z directions over a terrain, but i want it to stop when or near when the terrain stops. Ive tried a couple of things but overall they have been failures. I think I misunderstood what bounds are used for. Im still learning so any help is most appreciated.
     
  5. jeffcraighead

    jeffcraighead

    Joined:
    Nov 15, 2006
    Posts:
    740
    You should be able to put up 4 box colliders at each edge of the terrain and a box or sphere collider on the camera along with a rigidbody set to kinematic. Then the camera will get notified when the collision happens and you can stop moving the camera. This is just one way to do this quickly.
     
  6. Luke-Houlihan

    Luke-Houlihan

    Joined:
    Jun 26, 2007
    Posts:
    303
    But how do I tell the camera to stop? Im pretty new to scripting.
     
  7. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    I'd recommend just using Mathf.Clamp.

    Code (csharp):
    1. camPos.x = Mathf.Clamp(camPos.x, leftBoundary, rightBoundary);
    2. camPos.z = Mathf.Camp(campPos.z, nearBoundary, farBoundary);
    That's assuming you've got a variable called camPos that has the camera's transform (var camPos = Camera.main.transform would do it), and that you've made the various Boundary variables and assigned appropriate values to them. If your terrain is square and centered on the origin, then you could just use one variable that's half the width of the terrain.

    --Eric
     
  8. Luke-Houlihan

    Luke-Houlihan

    Joined:
    Jun 26, 2007
    Posts:
    303
    Code (csharp):
    1. var speed = 5.0;
    2.  
    3. function Update () {
    4.     var camPos = Camera.main.transform;
    5.     var leftBoundary = 1500;
    6.     var rightBoundary = 1600;
    7.     var nearBoundary = 1490;
    8.     var farBoundary = 910;
    9.     camPos.x = Mathf.Clamp(camPos.x, leftBoundary, rightBoundary);
    10.     camPos.y = Mathf.Clamp(camPos.z, nearBoundary, farBoundary);
    11.     var x = Input.GetAxis("Horizontal") * Time.deltaTime * speed;
    12.     var z = Input.GetAxis("Vertical") * Time.deltaTime * speed;
    13.     transform.Translate(x, 0, z);
    14. }
    Am I close? I know coding is not a strong point of mine yet.
    I get an error saying "x" is not a member of UnityEngine.transform
     
  9. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Somewhat close. :) The variables need to be global, so they'd be declared outside Update. This way you can also edit the values in the Inspector, as you probably know. Also, since it looks like your script is operating on the camera itself, there isn't really a need to get Camera.main after all (it should have been camPos.position.x anyway; sorry). The clamping should be done after the Translate, so the numbers stay within the range you've specified for that frame. Otherwise they'd be clamped the next frame, which probably would lead to some jittering if you tried to go past the boundaries.

    Code (csharp):
    1. var speed = 5.0;
    2. var leftBoundary = -1500;   // ?
    3. var rightBoundary = 1600;
    4. var nearBoundary = -1490;   // ?
    5. var farBoundary = 910;
    6.  
    7. function Update () {
    8.     var x = Input.GetAxis("Horizontal") * Time.deltaTime * speed;
    9.     var z = Input.GetAxis("Vertical") * Time.deltaTime * speed;
    10.     transform.Translate(x, 0, z);
    11.     transform.position.x = Mathf.Clamp(transform.position.x, leftBoundary, rightBoundary);
    12.     transform.position.z = Mathf.Clamp(transform.position.z, nearBoundary, farBoundary);
    13. }
    I also changed the leftBoundary and nearBoundary values to be negative; near has to be less than far in any case. I'm not sure what your terrain looks like, but leftBoundary is the lowest x value in world space that you can go to, nearBoundary is the lowest z value, right is highest x and far is highest z.

    --Eric
     
  10. Luke-Houlihan

    Luke-Houlihan

    Joined:
    Jun 26, 2007
    Posts:
    303
    :D That worked exactly like I wanted it to, thanks so much for the help. :D