using System.Collections; using System.Collections.Generic; using System.Linq; using UnityEngine; namespace Samkwang { public class ObjectTourController : MonoBehaviour { public Transform controlObject; public ObjectTourPoint[] tourPoints; public float moveSpeed = 2f; // À̵¿ ¼Óµµ (SmoothDampÀÇ maxSpeed ´ë½Å À̵¿¼Óµµ·Î »ç¿ë) public float turnSpeedDegrees = 120f; // ÃÊ´ç ÃÖ´ë ȸÀü °¢µµ (degree/sec) public float arrivalThreshold = 0.05f; // look-ahead: ¸ñÇ¥¿¡ °¡±î¿öÁú¶§ ´ÙÀ½ Æ÷ÀÎÆ® ¹æÇâÀ¸·Î ¹Ì¸® º¸µµ·Ï ÇÏ´Â °Å¸® public float lookAheadDistance = 1.0f; private int currentIndex = 0; private void Start() { controlObject = transform.Find("Walking_cart").transform; tourPoints = transform.GetComponentsInChildren().OrderBy(p => p.transform.GetSiblingIndex()).ToArray(); if (tourPoints.Length > 0) StartCoroutine(MoveTour()); } private IEnumerator MoveTour() { while (true) { yield return StartCoroutine(MoveToTarget(currentIndex)); currentIndex = (currentIndex + 1) % tourPoints.Length; } } private IEnumerator MoveToTarget(int index) { Transform target = tourPoints[index].transform; Transform nextTarget = tourPoints[(index + 1) % tourPoints.Length].transform; while (Vector3.Distance(controlObject.position, target.position) > arrivalThreshold) { // --- À§Ä¡ À̵¿ (SmoothDamp style) // SmoothDampÀÇ maxSpeed ÆÄ¶ó¹ÌÅÍ ´ë½Å ´Ü¼ø MoveTowards ±â¹ÝÀ¸·Î ¾ÈÁ¤Àû Á¦¾î float step = moveSpeed * Time.deltaTime; controlObject.position = Vector3.MoveTowards(controlObject.position, target.position, step); // --- ȸÀü: look-ahead ó¸® float distToTarget = Vector3.Distance(controlObject.position, target.position); // °¡±î¿öÁú¼ö·Ï ´ÙÀ½ Æ÷ÀÎÆ®¸¦ ´õ ¹Ù¶óº¸°Ô º¸°£ Vector3 desiredLookPos; if (distToTarget <= lookAheadDistance) { // º¸°£ ºñÀ²: 0(¸Ö¶§) -> 1(°¡±î¿ï¶§) float t = Mathf.InverseLerp(lookAheadDistance, 0f, distToTarget); Vector3 dirToCurrent = (target.position - controlObject.position).normalized; Vector3 dirToNext = (nextTarget.position - controlObject.position).normalized; Vector3 blendedDir = Vector3.Slerp(dirToCurrent, dirToNext, t).normalized; desiredLookPos = controlObject.position + blendedDir; } else { desiredLookPos = target.position; } Vector3 dir = (desiredLookPos - controlObject.position); if (dir.sqrMagnitude > 0.0001f) { Quaternion targetRot = Quaternion.LookRotation(dir.normalized); // ÃÊ´ç ȸÀü ÇѰ踦 Àû¿ë (´õ ÀÚ¿¬½º·¯¿î ȸÀü) float maxDeg = turnSpeedDegrees * Time.deltaTime; controlObject.rotation = Quaternion.RotateTowards(controlObject.rotation, targetRot, maxDeg); } yield return null; } // µµÂø ÈÄ Á¤È®È÷ À§Ä¡/ȸÀü Á¤·Ä (¼±ÅÃ) controlObject.position = target.position; } } }