using UnityEngine; /// ÀÌ TransformÀÇ À§Ä¡¸¦ ÁöÁ¤µÈ Áß½ÉÁ¡À¸·ÎºÎÅÍ ÃÖ´ë °Å¸®¸¦ ¹þ¾î³ªÁö ¾Êµµ·Ï Á¦ÇÑ public class TargetRangeLimiter : MonoBehaviour { [Header("Range Limit")] [SerializeField] private Transform centerPoint; [SerializeField] private float maxDistance = 2.0f; // LateUpdate¸¦ »ç¿ëÇÏ¿© ´Ù¸¥ ½ºÅ©¸³Æ®¿¡ ÀÇÇØ À§Ä¡°¡ º¯°æµÈ ÈÄ ÃÖÁ¾ÀûÀ¸·Î À§Ä¡ º¸Á¤ private void LateUpdate() { if (centerPoint == null) { return; } // 1. Áß½ÉÁ¡¿¡¼­ ÇöÀç ÇÚµé À§Ä¡±îÁöÀÇ º¤ÅÍ¿Í °Å¸®¸¦ °è»ê Vector3 directionFromCenter = transform.position - centerPoint.position; float currentDistance = directionFromCenter.magnitude; // 2. ÇöÀç °Å¸®°¡ ÃÖ´ë °Å¸®¸¦ ÃʰúÇß´ÂÁö È®ÀÎ if (currentDistance > maxDistance) { // 3. ¹æÇâÀº À¯ÁöÇÑ Ã¤, °Å¸®¸¦ ÃÖ´ë °Å¸®·Î Á¦ÇÑÇÏ´Â »õ·Î¿î À§Ä¡ °è»ê Vector3 newPosition = centerPoint.position + directionFromCenter.normalized * maxDistance; // 4. ÇÚµéÀÇ À§Ä¡¸¦ °è»êµÈ »õ À§Ä¡·Î °­Á¦ ¼³Á¤ transform.position = newPosition; } } //// ±âÁî¸ð¸¦ ±×·Á¼­ ÀÛ¾÷ ¹üÀ§¸¦ ½Ã°¢ÀûÀ¸·Î º¸¿©ÁÜ (¿¡µðÅÍ Àü¿ë) //private void OnDrawGizmosSelected() //{ // if (centerPoint != null) // { // Gizmos.color = Color.green; // Gizmos.DrawWireSphere(centerPoint.position, maxDistance); // } //} }