using UnityEngine; public class CameraDragMove : MonoBehaviour { public Camera cam; // 이동시킬 카메라 public SpriteRenderer targetSprite; // 영역 기준이 되는 스프라이트 public GameObject go_btnbg; public float dragSpeed = 1f; private Vector3 lastTouchPos; private Vector3 pressPos; // 입력 시작 지점 private bool isDragging = false; void Reset() { cam = Camera.main; } void Update() { #if UNITY_EDITOR || UNITY_STANDALONE HandleMouseInput(); // 에디터/PC #else HandleTouchInput(); // 모바일 #endif } void HandleMouseInput() { if (Input.GetMouseButtonDown(0)) { pressPos = Input.mousePosition; lastTouchPos = Input.mousePosition; isDragging = false; } else if (Input.GetMouseButton(0)) { Vector3 deltaScreen = Input.mousePosition - pressPos; isDragging = true; if (isDragging) { Vector3 deltaWorld = cam.ScreenToWorldPoint(Input.mousePosition) - cam.ScreenToWorldPoint(lastTouchPos); deltaWorld.z = 0f; cam.transform.position -= deltaWorld * dragSpeed; ClampCameraPosition(); lastTouchPos = Input.mousePosition; } } else if (Input.GetMouseButtonUp(0)) { } } void HandleTouchInput() { if (Input.touchCount == 1) { Touch touch = Input.GetTouch(0); switch (touch.phase) { case TouchPhase.Began: pressPos = touch.position; lastTouchPos = touch.position; isDragging = false; break; case TouchPhase.Moved: Vector3 deltaScreen = (Vector3)touch.position - pressPos; isDragging = true; if (isDragging) { Vector3 deltaWorld = cam.ScreenToWorldPoint(touch.position) - cam.ScreenToWorldPoint(lastTouchPos); deltaWorld.z = 0f; cam.transform.position -= deltaWorld * dragSpeed; ClampCameraPosition(); lastTouchPos = touch.position; } break; case TouchPhase.Ended: break; } } } void ClampCameraPosition() { if (targetSprite == null) return; Bounds spriteBounds = targetSprite.bounds; float camHeight = cam.orthographicSize * 2f; float camWidth = camHeight * cam.aspect; float minX = spriteBounds.min.x + camWidth / 2f; float maxX = spriteBounds.max.x - camWidth / 2f; float minY = spriteBounds.min.y + camHeight / 2f; float maxY = spriteBounds.max.y - camHeight / 2f; Vector3 pos = cam.transform.position; pos.x = Mathf.Clamp(pos.x, minX, maxX); pos.y = Mathf.Clamp(pos.y, minY, maxY); cam.transform.position = pos; } }