void Update(){
MoveCamera();
}
public bool IsPointerOverUIObject() {
PointerEventData ep = new PointerEventData(EventSystem.current);
ep.position = new Vector2(Input.mousePosition.x, Input.mousePosition.y);
List<RaycastResult> results = new List<RaycastResult>();
EventSystem.current.RaycastAll(ep, results);
return results.Count > 0;
}
private void MoveCamera(){
if(!IsPointerOverUIObject()){
if(Input.GetMouseButtonDown(0)){ CurrentPositionCamera = MainCamera.ScreenToWorldPoint(Input.mousePosition); }
if(Input.GetMouseButton(0)){ VerifyBoundCamera(); }else{ CameraMove = false; }
if(Input.GetMouseButtonUp(0)){ CameraMove = false; }
}else{ CameraMove = false; CurrentPositionCamera = MainCamera.ScreenToWorldPoint(Input.mousePosition); }
}
}
private void VerifyBoundCamera(){
PosInputMouse = MainCamera.ScreenToWorldPoint(Input.mousePosition);
DifferencePositionCamera = CurrentPositionCamera - PosInputMouse;
if(MainCamera.transform.position.y + DifferencePositionCamera.y > MaxY){
DifferencePositionCamera.y = 0f;
CurrentPositionCamera = new Vector3(CurrentPositionCamera.x, PosInputMouse.y, -1f);
}else if(MainCamera.transform.position.y + DifferencePositionCamera.y < MinY){
DifferencePositionCamera.y = 0f;
CurrentPositionCamera = new Vector3(CurrentPositionCamera.x, PosInputMouse.y, -1f);
}
if(MainCamera.transform.position.x + DifferencePositionCamera.x > MaxX){
DifferencePositionCamera.x = 0f;
CurrentPositionCamera = new Vector3(PosInputMouse.x, CurrentPositionCamera.y, -1f);
}else if(MainCamera.transform.position.x + DifferencePositionCamera.x < MinX){
DifferencePositionCamera.x = 0f;
CurrentPositionCamera = new Vector3(PosInputMouse.x, CurrentPositionCamera.y, -1f);
}
if(DifferencePositionCamera != PrevPositionCamera){
MainCamera.transform.position += DifferencePositionCamera;
PrevPositionCamera = DifferencePositionCamera;
CameraMove = true;
}else CameraMove = false;
}