# Camera Scrolling : Remaining within world boundries?

I am trying to implement an RTS game like camera for unity. I've made a simple project and created a Plane GameObject to use as the 'ground' or map bounding box for now. The Plane object Transform.position is at (0,-1,0) and it has a scale of (500, 1, 500). I am guessing that makes it 500 meters by 500 meters.

My camera is set in Orthographic mode at size 10, with transform.position at (0, 15, 0) rotation at (90,0,0) and Scale at (1,1,1).

My scrolling works the following way...

1- If mouse is detected within certain pixel width of the screen edge, then I need to initialize the scrolling in that direction.

2- My Plane ("LevelFloor") has a script attached to it that on start calculates the xMin, xMax, zMin, zMax values of the level (to be used as the bounds). This is done because the game is from an overhead perspective, so the Y axis is only for zooming in and out.

3- My scrolling code looks at the direction I need to scroll, then it takes camera.transform.postion as current coordinates, adds or subtracks ScrollSize values from the x and/or z coordinates (ScrollSize is a parameter to determine how much to scroll each time) and checks to see if the new coordinates are within the xMin, xMax, zMin, zMax parameters of the level. If so it adjusts the camera transform to the new coordinates.

Here is my code....

``````private Vector3 currentCoordinates;
private Vector3 newCoordinates;

private float _xMin;
private float _xMax;
private float _zMin;
private float _zMax;

private float scrollX;
private float scrollZ;

void Start()
{
level = GameObject.FindGameObjectWithTag("LevelFloor");
if (level == null)
Debug.LogError("No Level Floor found. There beeds to be a game object with the Tag Level floor in each scene");

_xMin = level.transform.localPosition.x;
_xMax = _xMin + level.transform.localScale.x;

_zMin = level.transform.localPosition.z;
_zMax = _zMin + level.transform.localScale.z;
}

public void Scroll(string direction)
{
Debug.Log("Direction : " + direction);
bool canScroll = true;

switch (direction)
{
case "Left":
scrollX = -cameraAttributes.ScrollSize;
scrollZ = 0.0f;
break;
case "Right":
scrollX = cameraAttributes.ScrollSize;
scrollZ = 0.0f;
break;
case "Up":
scrollZ = cameraAttributes.ScrollSize;
scrollX = 0.0f;
break;
case "Down":
scrollZ = -cameraAttributes.ScrollSize;
scrollX = 0.0f;
break;
}

currentCoordinates = gameObject.camera.transform.position;
Debug.Log("Curr: " + currentCoordinates);
newCoordinates = new Vector3(currentCoordinates.x + scrollX, currentCoordinates.y, currentCoordinates.z + scrollZ);

Debug.Log("New : " + newCoordinates);
Debug.Log(levelAttributes);

if (((newCoordinates.x <= xMax) && (newCoordinates.x >= xMin))
&& ((newCoordinates.z <= zMax) && (newCoordinates.z >= zMin)))
{
camera.transform.position = newCoordinates;
}
}

``````

The problem I am having is that the camera coordinates seem to be 0,0,0 close to the absolute middle of the plane(LevelFloor). The floor returns its xmin, zmin as 0,0 and so I cant seem to scroll left or down much more than the middle of the plane(LevelFloor). When I scroll top or left then it always overshoots the level and ends up outside of it showing me a blue background.

I am afraid im not doing a good job of explaining what my problem is, or what im trying to achieve, but can anyone see what I am doing wrong here?

Not delved too deep into your code but shouldn't:

``````if (((newCoordinates.x <= xMax) && (newCoordinates.x >= xMin))
&& ((newCoordinates.z <= zMax) && (newCoordinates.z >= zMin)))

``````

be:

``````if (((newCoordinates.x <= _xMax) && (newCoordinates.x >= _xMin))
&& ((newCoordinates.z <= _zMax) && (newCoordinates.z >= _zMin)))

``````