How do I keep Camera within borders?

Hey there!

Okay, so long story short, I have to make a game for a school project.
I’ve hit a problem. it’s a sidescroller game, but it’s 3d. I’ve made a script for the
camera to pan with middle mouse button, but my problem is that I want the camera
that I’m controlling to stay within borders, so that you can’t look under the world and so on.

Here a little screenshot to show you want I mean

Imgur: The magic of the Internet

Ps. I kinda suck at scripting, so if you could really explain it very well I would really appreciate that!:9

Friendly Regards

EsIndesign

You could say in the update function (after the calculation of the camera movement) that the X Y of the camera can’t be higher or lower then a sertain point. For example:

if(cameraPositionX < 0)
	cameraPositionX = 0;
else if (cameraPositionx > 100)
	cameraPositionX = 100;

This way the camera position x cant be lower than 0 or greater than 100.
This code does not work but you should get the idea. If you really can’t figure it out just post your script :slight_smile:

Field of view, setting of the camera.

Here is the script I use for side scrolling the camera like pan

var moveSpeed : float = .5;

function Update () {

if (Input.GetMouseButton(2)) {
transform.Translate(Vector3.right * -Input.GetAxis(“Mouse X”) * moveSpeed);
transform.Translate(transform.up * -Input.GetAxis(“Mouse Y”) * moveSpeed, Space.World);
}

}

Have you even tried? :wink:

This should work

var moveSpeed : float = .5;
var maxPos : Vector2; // Dont forget to fill this in in the inspector fiew
var minPos : Vector2; // Dont forget to fill this in in the inspector fiew

function Update () {	
	if (Input.GetMouseButton(2)) {
		transform.Translate(Vector3.right * -Input.GetAxis("Mouse X") * moveSpeed);
		transform.Translate(transform.up * -Input.GetAxis("Mouse Y") * moveSpeed, Space.World);
		
		// you could also place the locking of the position here, but then it would only be updated when you pressed a key, now it will also keep restricted if somehow another script tries to move it
	}
	
	// We place this after you've updated the transform, so it gets corrected right away. If you would place this above the adjustment of the position it would be posible to be out of range for 1 frame
	if(transform.position.x < minPos.x) // If the x position is lower than minPos.x , set the position to minPos.x
		transform.position.x = minPos.x;
	else if(transform.position.x > maxPos.x) // else If the x position is higher than maxPos.x , set the position to maxPos.x
		transform.position.x = maxPos.x;
	if(transform.position.y < minPos.y) // If the y position is lower than minPos.y , set the position to minPos.y
		transform.position.y = minPos.y;
	else if (transform.position.y > maxPos.y) // else If the y position is higher than maxPos.y , set the position to maxPos.y
		transform.position.y = maxPos.y;
}

Omg! thank you SOOO MUCH!
this work perfectly!

THANK YOU!

Your welcome :wink:

Or make it easier and just do

cameraPositionX = Mathf.Clamp(cameraPositionX, 0, 100);

http://unity3d.com/support/documentation/ScriptReference/Mathf.Clamp.html

Oh sweet! I wish I would have know that before! :stuck_out_tongue:
You learn something everyday :wink: Thanks!