Best way to stop the camera from at edge of level

I have a game level that is just a plane with the camera above the player looking down. The player can go to the edge of the level but I don’t want the camera seeing past the edge. If you have every played miniGORE for the iPhone, I’m trying to set the camera up like that.

What is the best way of doing this?

The attached photo shows the camera not moving past a certain point and the character not moving past a “box collider”.

223449--8080--$img_0441_126.png

Just find the minimum and maximum positions on both sides and clamp the transform to keep from going beyond those positions.

var minPosition : float = -10.0; // left border
var maxPosition : float = 10.0; //  right border

function Update() {
    transform.position.x = Mathf.Clamp(transform.position.x, minPosition, maxPoosition);
}

Something like that.

-Raiden

Thanks for the reply. Is the min/max positions taken from the size of the level? And this script gets added to the camera? :sweat_smile:

The min and max positions would be the borders of your level, for example in my Breakout clone game, I figured out the borders by moving the paddle as far as I want to go to the left:

You can see in the Transform “x” position I know I’m going to clamp my paddle from moving no further to the left @ -12, which will be the minimum amount in the clamp, then you just move your object all the way to the right you want to go, and that will be the maximum value in the clamp. Just use the positions in the Transform and you will be able to find your limits. Oh, and yes you would use this script on your camera.

-Raiden

Raiden, thanks so much.

Yeah, so i used this code. but:
CS1612 C# Cannot modify the return value of ‘Transform.position’ because it is not a variable