using Sirenix.OdinInspector; using UnityEngine; using UnityEngine.AI; // ReSharper disable once CheckNamespace namespace BlueWaterProject { public class ShipPatrol : MonoBehaviour, IPatrol { /*********************************************************************** * Variables ***********************************************************************/ #region Variables // 컴포넌트 [Title("컴포넌트")] [SerializeField] private Rigidbody rb; // 패트롤 옵션 [Title("패트롤 옵션")] [SerializeField] private bool showWayPointGizmo = true; [field: SerializeField] public WayPoint[] WayPoints { get; set; } [field: SerializeField] public Vector3 OriginalPoint { get; set; } [field: SerializeField] public int CurrentIndex { get; set; } [field: SerializeField] public int PreviousIndex { get; set; } // 기즈모 잠금 기능 [Title("기즈모 잠금 기능")] [field: SerializeField] public bool LockHandlesOnXAxis { get; set; } [field: SerializeField] public bool LockHandlesOnYAxis { get; set; } [field: SerializeField] public bool LockHandlesOnZAxis { get; set; } #endregion /*********************************************************************** * Unity Events ***************************************************************m********/ #region Unity Events private void OnDrawGizmosSelected() { if (WayPoints == null || WayPoints.Length <= 0 || !showWayPointGizmo) return; if (!Application.isPlaying) { OriginalPoint = rb.position; } for (var i = 0; i < WayPoints.Length; i++) { var currentColor = i == CurrentIndex ? Color.blue : Color.yellow; if ((i + 1) < WayPoints.Length) { Gizmos.color = currentColor; Gizmos.DrawLine(OriginalPoint + WayPoints[i].Point, OriginalPoint + WayPoints[i + 1].Point); } if (i == WayPoints.Length - 1) { Gizmos.color = currentColor; Gizmos.DrawLine(OriginalPoint + WayPoints[i].Point, OriginalPoint + WayPoints[0].Point); } } if (!Application.isPlaying) return; DrawGizmoPoint(OriginalPoint + WayPoints[CurrentIndex].Point,2f, Color.blue); DrawGizmoPoint(OriginalPoint + WayPoints[PreviousIndex].Point, 2f, Color.red); } private static void DrawGizmoPoint(Vector3 pos, float size, Color color) { Gizmos.color = color; Gizmos.DrawWireSphere(pos, size); } private void Start() { OriginalPoint = transform.position; } #endregion /*********************************************************************** * Methods ***********************************************************************/ #region Methods public WayPoint GetCurrentWayPoint() => WayPoints[CurrentIndex]; public int GetNextIndex() { if (WayPoints == null) return -1; var maxSize = WayPoints.Length; var nextIndex = CurrentIndex + 1; if (nextIndex >= maxSize) { nextIndex = 0; } return nextIndex; } public bool HasReachedDestination() { var myPosition = rb.position; var movePoint = OriginalPoint + WayPoints[CurrentIndex].Point; var distance = Vector3.Distance(myPosition, movePoint); print(distance); if (distance > 3f) return false; print("도착"); return true; } public void SetMovePoint() { if (!rb || WayPoints == null || WayPoints.Length <= CurrentIndex) return; print("다음 목적지"); PreviousIndex = CurrentIndex; CurrentIndex = GetNextIndex(); } public void UpdatePositionAndRotation() { // // 현재 위치에서 목표지점까지의 방향과 거리 계산 // var myPosition = rb.position; // var movePoint = OriginalPoint + WayPoints[CurrentIndex].Point; // var direction = (movePoint - myPosition).normalized; // var distance = Vector3.Distance(movePoint, myPosition); // // // Combine forces with weights // var currentVelocity = seekForce + avoidForce; // currentVelocity = Vector3.ClampMagnitude(currentVelocity, maxVelocity); // // if (distance < 10f) // { // // 목적지에 가까워짐에 따라 속도를 동적으로 조절 // var desiredSpeed = (distance / 10f) * maxSpeed; // currentVelocity = currentVelocity.normalized * desiredSpeed; // } // else // { // // 목적지에서 멀리 떨어져 있을 때는 최대 속도로 이동 // currentVelocity = Vector3.ClampMagnitude(currentVelocity, maxSpeed); // } // // rb.AddForce(currentVelocity - rb.velocity, ForceMode.Acceleration); // // //rb.AddForce(currentVelocity, ForceMode.Acceleration); // // if (rb.velocity.magnitude > 10f) // { // rb.velocity = rb.velocity.normalized * 10f; // } // // var targetRotation = Quaternion.LookRotation(direction); // rb.MoveRotation(Quaternion.RotateTowards(rb.rotation, targetRotation, rotationSpeed * Time.deltaTime)); } #endregion } }