# Need Help adding camera position constraints

Hi, I am trying to set constraints on how far my camera can move on both the x and y axis. I am primarily a 3D content developer with very minimal coding experience. Here is what I have and it works, just need a way to constraints so the camera is bound to a certain area. THANKS!

``````using UnityEngine;
using System.Collections;

public class CameraDrag : MonoBehaviour {

Camera cam;
Vector3 oldPos;
Vector3 pos;
Vector3 deltaPos;

// Use this for initialization
void Start() {
cam = Camera.main;
Debug.Log("start");
}

// Update is called once per frame
void Update() {
if (Input.GetMouseButtonDown(2)) {
Vector3 mousePos = Input.mousePosition;
mousePos.z = 1;

oldPos = cam.ScreenToWorldPoint(mousePos);
}

if (Input.GetMouseButton(2)) {
Vector3 mousePos = Input.mousePosition;
mousePos.z = 1;

pos = cam.ScreenToWorldPoint(mousePos);

deltaPos = new Vector3(oldPos.x - pos.x, oldPos.y - pos.y, oldPos.z - pos.z);
cam.transform.position += deltaPos;
}

if (Input.GetMouseButtonDown(2)) {
oldPos = pos;
}
}
}
``````

Yo can try this:

``````deltaPos = new Vector3(oldPos.x - pos.x, oldPos.y - pos.y, oldPos.z - pos.z);

Vector3 newPos = cam.transform.position + deltaPos;

Vector3 newPosClamped = new Vector3(Mathf.Clamp(newPos.x, minX, maxX), Mathf.Clamp(newPos.y, minY, maxY), Mathf.Clamp(newPos.z, minZ, maxZ));

cam.transform.position = newPosClamped;
``````

I was able to finally figure it out. Here is my completed script. Thanks so much for the help!

``````using UnityEngine;
using System.Collections;

public class CameraDrag : MonoBehaviour {

Camera cam;
Vector3 oldPos;
Vector3 pos;
Vector3 deltaPos;

// Use this for initialization
void Start() {
cam = Camera.main;
Debug.Log("start");
}
public float minX, maxX, minY, maxY, minZ, maxZ;
// Update is called once per frame
void Update() {
if (Input.GetMouseButtonDown(2)) {
Vector3 mousePos = Input.mousePosition;
mousePos.z = 1;

oldPos = cam.ScreenToWorldPoint(mousePos);
}

if (Input.GetMouseButton(2)) {
Vector3 mousePos = Input.mousePosition;
mousePos.z = 1;

pos = cam.ScreenToWorldPoint(mousePos);

deltaPos = new Vector3(oldPos.x - pos.x, oldPos.y - pos.y, oldPos.z - pos.z);

Vector3 newPos = cam.transform.position + deltaPos;

Vector3 newPosClamped = new Vector3(Mathf.Clamp(newPos.x, minX, maxX), Mathf.Clamp(newPos.y, minY, maxY), Mathf.Clamp(newPos.z, minZ, maxZ));

cam.transform.position = newPosClamped;
}

if (Input.GetMouseButtonDown(2)) {
oldPos = pos;
}
}
}
``````
{"ft_lang_label":"__label__en","ft_lang_prob":0.54114056,"math_prob":0.96754795,"size":2387,"snap":"2023-40-2023-50","text_gpt3_token_len":623,"char_repetition_ratio":0.15442719,"word_repetition_ratio":0.64396286,"special_character_ratio":0.26811898,"punctuation_ratio":0.29327902,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9801606,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2023-09-24T11:06:06Z\",\"WARC-Record-ID\":\"<urn:uuid:90611168-9f14-46ad-b3be-375ac47e060e>\",\"Content-Length\":\"24752\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:7666d730-8697-478f-b264-1c328652765e>\",\"WARC-Concurrent-To\":\"<urn:uuid:2ad6491d-bf4d-44a8-81ca-0db1d0f1b780>\",\"WARC-IP-Address\":\"184.104.178.75\",\"WARC-Target-URI\":\"https://discussions.unity.com/t/need-help-adding-camera-position-constraints/150745\",\"WARC-Payload-Digest\":\"sha1:R5GBE5HQMLVW4TRFYVG75ZM63EFUFXB7\",\"WARC-Block-Digest\":\"sha1:NBU6VDEICBIV3JTHB5ZN7J3V6WNK4OTC\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2023/CC-MAIN-2023-40/CC-MAIN-2023-40_segments_1695233506632.31_warc_CC-MAIN-20230924091344-20230924121344-00147.warc.gz\"}"}