51 lines
1.3 KiB
C#
51 lines
1.3 KiB
C#
using UnityEngine;
|
|
|
|
public class SeaCreatureWaypoint : MonoBehaviour
|
|
{
|
|
[Header("Waypoint Settings")]
|
|
public Transform[] waypoints;
|
|
|
|
[Header("Movement Settings")]
|
|
public float moveSpeed = 2f;
|
|
public float rotationSpeed = 5f;
|
|
public float arriveDistance = 0.2f;
|
|
|
|
private int currentWaypoint = 0;
|
|
|
|
void Update()
|
|
{
|
|
if (waypoints == null || waypoints.Length == 0)
|
|
return;
|
|
|
|
Transform target = waypoints[currentWaypoint];
|
|
|
|
// 이동
|
|
transform.position = Vector3.MoveTowards(
|
|
transform.position,
|
|
target.position,
|
|
moveSpeed * Time.deltaTime);
|
|
|
|
// 바라보는 방향
|
|
Vector3 direction = target.position - transform.position;
|
|
|
|
if (direction != Vector3.zero)
|
|
{
|
|
Quaternion targetRotation = Quaternion.LookRotation(direction);
|
|
transform.rotation = Quaternion.Slerp(
|
|
transform.rotation,
|
|
targetRotation,
|
|
rotationSpeed * Time.deltaTime);
|
|
}
|
|
|
|
// 도착하면 다음 웨이포인트
|
|
if (Vector3.Distance(transform.position, target.position) < arriveDistance)
|
|
{
|
|
currentWaypoint++;
|
|
|
|
if (currentWaypoint >= waypoints.Length)
|
|
{
|
|
currentWaypoint = 0;
|
|
}
|
|
}
|
|
}
|
|
} |