using UnityEngine; // ติดกับกล้องหลักเพื่อไล่ตามเป้าหมาย และจำกัดภาพไม่ให้ออกนอกขอบสนาม public class CameraFollow : MonoBehaviour { public Transform target; public float smoothSpeed = 5f; public Vector3 offset = new Vector3(0, 0, -10f); public Camera cam; // Camera Bounds — กล้องจะไม่หลุดออกนอกขอบแม็พ public Vector2 arenaMin = new Vector2(-38f, -35.5f); public Vector2 arenaMax = new Vector2(38f, 35.5f); void Start() { int mapIndex = GameplayManager.GetCurrentMapIndex(); arenaMin = GameplayManager.GetArenaMin(mapIndex); arenaMax = GameplayManager.GetArenaMax(mapIndex); cam = GetComponent(); if (cam != null) { cam.orthographicSize = 10f; // ซูมออกให้เห็นกว้างขึ้น } } void LateUpdate() { if (target == null) { // พยายามหาเป้าหมาย if (GameplayManager.Instance != null && GameplayManager.Instance.localPlayer != null) { target = GameplayManager.Instance.localPlayer.transform; } return; } Vector3 desiredPosition = target.position + offset; Vector3 smoothedPosition = Vector3.Lerp(transform.position, desiredPosition, smoothSpeed * Time.deltaTime); if (CameraShake.Instance != null) { smoothedPosition += CameraShake.Instance.currentShakeOffset; } transform.position = ClampToArena(smoothedPosition); } // Clamp Camera Position เพื่อไม่ให้กล้องโชว์นอกแม็พ private Vector3 ClampToArena(Vector3 position) { if (cam != null) { float camHeight = cam.orthographicSize; float camWidth = camHeight * cam.aspect; position.x = Mathf.Clamp(position.x, arenaMin.x + camWidth, arenaMax.x - camWidth); position.y = Mathf.Clamp(position.y, arenaMin.y + camHeight, arenaMax.y - camHeight); } return position; } // กระโดดไปที่ยานทันที (ใช้หลังวาร์ป ไม่ให้กล้องเลื่อนข้ามแม็พ) public void SnapToTarget() { if (target == null) return; transform.position = ClampToArena(target.position + offset); } }