# Limit Camera Movement on XY Axis

Discussion in 'Scripting' started by Hansuke, Jun 26, 2007.

1. ### Hansuke

Joined:
Apr 17, 2007
Posts:
100
Code (csharp):
1. var mDelta = 10; // Pixels. The width border at the edge in which the movement work
2. var mSpeed = 30.0; // Scale. Speed of the movement
3. var xSensitivity = 20.0;
4. var ySensitivity = 20.0;
5.
6. function LateUpdate ()
7. {
8.
9.       // Check if on the right edge
10.    if ( Input.mousePosition.x >= Screen.width - mDelta )
11.    {
12.       // Move the camera
13.       transform.position += transform.right * Time.deltaTime * mSpeed;
14.    }
15.
16.
17.    if ( Input.mousePosition.x <= 0 + mDelta )
18.    {
19.       // Move the camera
20.       transform.position -= transform.right * Time.deltaTime * mSpeed;
21.    }
22.
23.
24.    if ( Input.mousePosition.y >= Screen.height - mDelta )
25.    {
26.       // Move the camera
27.       transform.position += transform.forward * Time.deltaTime * mSpeed;
28.    }
29.
30.    if ( Input.mousePosition.y <= 0 + mDelta )
31.    {
32.       // Move the camera
33.       transform.position -= transform.forward * Time.deltaTime * mSpeed;
34.    }
35.
36.
37.     // Changing an angle, if mouse button is held
38.     if (Input.GetMouseButton(1) )
39.     {
40.       // Update x, y angle with the mouse delta
41.       xAngleChange = Input.GetAxis ("Mouse X") * xSensitivity * Time.deltaTime;
42.       yAngleChange = Input.GetAxis ("Mouse Y") * ySensitivity * Time.deltaTime;
43.
44.       // Rotate around the current up direction by the delta mouse x
45. transform.rotation = transform.rotation *
46.          Quaternion.Euler(-yAngleChange, xAngleChange, 0);
47.    }
48. }
this is my code for my camera movement...how to set a limit in the X and Y axis? Now the camera can keep scrolling infinity...I want to set the camera to move within an area only.

Example: when i have a 300 x 300 box, the camera can only move around that box, not outside the box

2. ### StarManta

Joined:
Oct 23, 2006
Posts:
8,777
Simplest way is to use Clamp:

Code (csharp):
1.
2. transform.position.x = Mathf.Clamp(-100, 100);
3.

3. ### Hansuke

Joined:
Apr 17, 2007
Posts:
100
Thanks, i'll try to experiment it. By the way, is there any example that i can look at ?

EDIT: Thanks its working now, need to add transform.position.x inside the bracket.

Code (csharp):
1. transform.position.x = Mathf.Clamp(transform.position.x,-10, 10);

4. ### simjeanny88

Joined:
May 17, 2012
Posts:
19
Hi! I have problem with camera 2d.
1. I have a big map and camera screen smaller than map.
2. I want camera move follow the player and player always center of the camera screen, but if the camera move to the end of right map or bottom of the map or top of the map, the camera will not move follow the player , player can move to the end of right map without move camera.
This is my script:

var cam = Camera.main.transform;
if(transform.position.x >= -20 transform.position.x <= 20 || transform.position.y <= 18.5 transform.position.y >= 0 ){
cam.parent=transform;
}
else{
cam.parent = null;
}

some pic :

Last edited: Jun 8, 2012