- Home /
Restrict 2d drag camera movement
Hi i got this script:
using UnityEngine;
using System.Collections;
public class CameraMovement : MonoBehaviour {
private float dist;
private Vector3 v3OrgMouse;
private Plane plane = new Plane(Vector3.up, Vector3.zero);
void Start() {
dist = transform.position.z; // Distance camera is above map
}
void Update() {
if (Input.GetMouseButtonDown(0)) {
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
float dist;
plane.Raycast(ray, out dist);
v3OrgMouse = ray.GetPoint(dist);
v3OrgMouse.z = 0;
} else if (Input.GetMouseButton(0)) {
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
float dist;
plane.Raycast(ray, out dist);
Vector3 v3Pos = ray.GetPoint(dist);
v3Pos.z = 0;
transform.position -= (v3Pos - v3OrgMouse);
}
}
}
I need borders, i tried something like this:
if (transform.position.x >= -320 && transform.position.y <= 180 && transform.position.x <= 320 && transform.position.y >= -180) {
transform.position -= (v3Pos - v3OrgMouse);
}
But with my script i can move the camera for example to x = -321 and then i cant move anything anymore because the if statement is false.
I hope you have a solution. Thx
Comment
Best Answer
Answer by JatsArts · Jun 12, 2016 at 01:20 PM
This is what I normally do.
transform.position = new Vector3(
Mathf.Clamp(transform.position.x,-320,320),
Mathf.Clamp(transform.position.y,-180,180),
transform.position.z
);