Mathf.Clamp problem for my drag / zoom camera

I’m new to unity and programming, and I’m trying to create a 2.5D strategy game for IOS. I want the camera to be exactly like the clash of clans camera (touch and drag camera with zooming and boundaries). If you just happened to have full code that I could attach to my camera and have it work magically, I wouldn’t complain :wink:

My terrain goes along the XZ plane. I want my camera to hover above ground, and move along the XZ plane when 1 finger is dragged. I have this part down. I still need to add in pinch zooming and boundaries.

Here’s my current question: I want my clamp to hard-stop the camera from going past the bounds. Currently, with a transform.position.x boundary of -62, the camera goes to -63 and then snaps back to -62. This creates a bouncing motion whenever I reach the edges of the playing field. I want the camera to stop when it hits the boundaries, with no snap back. Any way I can modify the clamp to hard-stop the camera at the points I’ve set?

If not, any advice on alternative methods to creating 3d boundaries for my camera? Thanks for your patience and help!

#pragma strict

var speed : float = .075;

function Update () {

// Clamp
transform.position = new Vector3(
           Mathf.Clamp(transform.position.x, -62.0, 62), 
           Mathf.Clamp(transform.position.y, 8.7, 8.7),
           Mathf.Clamp(transform.position.z, -3.5, -3.5));

// for Iphone - Detects moving finger
if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Moved) {
	
	// Get movement of the finger since last frame
	var touchDeltaPosition: Vector2 = Input.GetTouch(0).deltaPosition;
	
	// move object across XZ plane
	transform.Translate (-touchDeltaPosition.x * speed, 0,
                             -touchDeltaPosition.y * speed);
}

}

You should reverse the order: read touches first, then clamp position - like this:

function Update(){
  // for Iphone - Detects moving finger
  if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Moved) {
    // Get movement of the finger since last frame
    var touchDeltaPosition: Vector2 = Input.GetTouch(0).deltaPosition;
    // move object across XZ plane
    transform.Translate (-touchDeltaPosition.x * speed, 0, -touchDeltaPosition.y * speed);
  }
  transform.position = new Vector3(
           Mathf.Clamp(transform.position.x, -62.0, 62), 
           Mathf.Clamp(transform.position.y, 8.7, 8.7),
           Mathf.Clamp(transform.position.z, -3.5, -3.5));
}