Camera Bounds

Hi, I’ve got a Camera script that makes the camera move by dragging your mouse along the ground’s surface. This works fine all in all, but I’ve ran into some difficulties. It’s a topdown Perspective Camera and I wouldn’t know how to set boundaries.

I don’t want the camera to be able to exit a certain range. I’ve tried multiple approaches, but I can’t get it to work.

You need to clamp the position of the camera in your update routine.

Define some variables:

 var minCamera : Vector3;
var maxCamera: Vector3;

In Update() do:

transform.position = Vector3(
    Mathf.Clamp(transform.position.x, minCamera.x, maxCamera.x), 

    Mathf.Clamp(transform.position.y, minCamera.y, maxCamera.y), 

    Mathf.Clamp(transform.position.z, minCamera.z, maxCamera.z));

Set your min and max variables and away you go

If you don’t need to worry about one of the coordinates, (maybe Y for example) just remove the Mathf.Clamp() and use transform.position.y (or whatever)

It seems like you need…a conditional statement!!!…and…an empty game object!!!

Back to seriousness. Create the empty game object wherever the center of the game area will be. This object will define where the camera can go. Also, you will need some coding (here it is in Javascript):

var center : gameObject //the empty game object referred above
var distance : Vector3 = Vector3.Distance(center,gameObject) 

/*gameObject refers to
self.  If this script is not attached to the camera, set up another variable (like
center) for the camera.*/

var edgefactor : int //where is the edge of the game world?

// [skip to wherever the function for the camera begins]

if (distance < edgefactor)
{

// [insert the stuff that sets up movement here]

}
else
{

// Do nothing!

}

// [insert the stuff that actually moves the camera]