Hi,
I'm trying to write a script that creates clamps for the X and Y coordinates of an orthographic camera when panning around a map (UISprite). I am trying to set it up so that the camera bounds never leave the bounds of the map (i.e. so no background is shown). The Size of the orthographic camera will also change as I plan to integrate zooming later on. I have adapted a script that many people use for this purpose but am having some trouble getting it to work properly.
As the Size of the map is stored in the UISprite component and not in world coordinates, it is clamping at the wrong point. The clamp on the X stops just under half the width of the camera over the map bounds, and the Y stops at the camera's centre (so half in, half out). To clarify, in order to work correctly the bounds of the camera should stop at the bounds of the map, no matter its size.
I have pasted the script below. The Size of the map in its UPSprite is 1280 x 800, the size of the camera is 1 (but will change, so should not make a difference).
Any help would be fantastic as this one has stumped me!

using UnityEngine;
using System.Collections;
public class ChangeMapSize : MonoBehaviour {
private float minX;
private float maxX;
private float minY;
private float maxY;
private float mapW;
private float mapH;
private Transform Map;
void Start()
{
// Reference to the map transform
Map = GameObject.FindGameObjectWithTag(Tags.Map).transform;
// Access public static members to retrieve the map size
mapW = FindMapSize.MapWidth; // 1280
mapH = FindMapSize.MapHeight; // 800
// Calculate the vertical size of the camera
float vertExtent = transform.camera.orthographicSize;
// Calculate the horizontal size of the camera in screen space
float horzExtent = vertExtent * Screen.width / Screen.height;
// Calculations assume map is position at the origin
minX = horzExtent - mapH / 2.0f;
maxX = mapH / 2.0f - horzExtent;
minY = vertExtent - mapH / 2.0f;
maxY = mapH / 2.0f - vertExtent;
}
void LateUpdate()
{
Vector3 v3 = transform.localPosition;
v3.x = Mathf.Clamp(v3.x, minX, maxX);
v3.y = Mathf.Clamp(v3.y, minY, maxY);
transform.localPosition = v3;
}
}