RandomGFGoStop/Assets/Scripts/My/CameraDragMove.cs

96 lines
2.7 KiB
C#
Raw Normal View History

2025-08-29 14:29:11 +00:00
using UnityEngine;
public class CameraDragMove : MonoBehaviour
{
public ShowPanel m_ShowPanel;
2025-08-29 14:29:11 +00:00
public Camera cam; // 이동시킬 카메라
public SpriteRenderer targetSprite; // 영역 기준이 되는 스프라이트
public GameObject go_btnbg;
public float dragSpeed = 1f;
private Vector3 lastTouchPos;
private Vector3 pressPos; // 입력 시작 지점
Vector3 lastcamPos;
2025-08-29 14:29:11 +00:00
private bool isDragging = false;
void Reset()
{
cam = Camera.main;
}
void Update()
{
#if UNITY_EDITOR || UNITY_STANDALONE
if (Input.GetMouseButtonDown(0) || Input.GetMouseButton(0) || Input.GetMouseButtonUp(0))
{
ProcessInput(
Input.GetMouseButtonDown(0),
Input.GetMouseButton(0),
Input.GetMouseButtonUp(0),
Input.mousePosition
);
}
2025-08-29 14:29:11 +00:00
#else
if (Input.touchCount == 1)
{
Touch touch = Input.GetTouch(0);
ProcessInput(
touch.phase == TouchPhase.Began,
touch.phase == TouchPhase.Moved,
touch.phase == TouchPhase.Ended,
touch.position
);
}
2025-08-29 14:29:11 +00:00
#endif
}
void ProcessInput(bool began, bool moved, bool ended, Vector3 position)
2025-08-29 14:29:11 +00:00
{
if (!m_ShowPanel.isFull) return;
if (began)
2025-08-29 14:29:11 +00:00
{
pressPos = position;
lastTouchPos = position;
lastcamPos = cam.transform.position;
2025-08-29 14:29:11 +00:00
isDragging = false;
}
else if (moved)
2025-08-29 14:29:11 +00:00
{
Vector3 deltaWorld = cam.ScreenToWorldPoint(position) - cam.ScreenToWorldPoint(lastTouchPos);
deltaWorld.z = 0f;
2025-08-29 14:29:11 +00:00
cam.transform.position -= deltaWorld * dragSpeed;
ClampCameraPosition();
2025-08-29 14:29:11 +00:00
lastTouchPos = position;
2025-08-29 14:29:11 +00:00
if (lastcamPos != cam.transform.position || Mathf.Abs(deltaWorld.x) >= 0.01f || Mathf.Abs(deltaWorld.y) >= 0.01f)
isDragging = true;
2025-08-29 14:29:11 +00:00
}
else if (ended)
2025-08-29 14:29:11 +00:00
{
if (!isDragging)
m_ShowPanel.OnClick_Btn(4);
2025-08-29 14:29:11 +00:00
}
}
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;
}
}