217 lines
4.7 KiB
C#
217 lines
4.7 KiB
C#
using UnityEngine;
|
|
using UnityEngine.AI;
|
|
|
|
public class MinionRouteMover : MonoBehaviour
|
|
{
|
|
[Header("Route Points")]
|
|
public Transform[] routePoints;
|
|
|
|
[Header("Move Type")]
|
|
public bool pingPong = true;
|
|
public bool loop = true;
|
|
|
|
[Header("Move Setting")]
|
|
public float arriveDistance = 0.3f;
|
|
public float waitTimeAtPoint = 0.5f;
|
|
|
|
[Header("Random Setting")]
|
|
public bool randomStartDelay = true;
|
|
public float maxStartDelay = 2f;
|
|
|
|
public bool randomSpeed = true;
|
|
public float minSpeed = 1.8f;
|
|
public float maxSpeed = 3.0f;
|
|
|
|
public bool randomAnimationSpeed = true;
|
|
public float minAnimSpeed = 0.9f;
|
|
public float maxAnimSpeed = 1.15f;
|
|
|
|
private NavMeshAgent agent;
|
|
private Animator animator;
|
|
|
|
private int currentIndex = 0;
|
|
private int direction = 1;
|
|
|
|
private bool started = false;
|
|
private bool waiting = false;
|
|
|
|
private float startTimer = 0f;
|
|
private float waitTimer = 0f;
|
|
|
|
void Start()
|
|
{
|
|
agent = GetComponent<NavMeshAgent>();
|
|
animator = GetComponent<Animator>();
|
|
|
|
if (agent == null)
|
|
{
|
|
Debug.LogWarning(name + " : NavMeshAgent ¾øÀ½");
|
|
enabled = false;
|
|
return;
|
|
}
|
|
|
|
if (routePoints == null || routePoints.Length == 0)
|
|
{
|
|
Debug.LogWarning(name + " : Route Points ¾øÀ½");
|
|
enabled = false;
|
|
return;
|
|
}
|
|
|
|
if (randomSpeed)
|
|
{
|
|
agent.speed = Random.Range(minSpeed, maxSpeed);
|
|
}
|
|
|
|
if (randomAnimationSpeed && animator != null)
|
|
{
|
|
animator.speed = Random.Range(minAnimSpeed, maxAnimSpeed);
|
|
}
|
|
|
|
if (randomStartDelay)
|
|
{
|
|
startTimer = Random.Range(0f, maxStartDelay);
|
|
}
|
|
else
|
|
{
|
|
startTimer = 0f;
|
|
}
|
|
|
|
agent.isStopped = true;
|
|
}
|
|
|
|
void Update()
|
|
{
|
|
if (!started)
|
|
{
|
|
startTimer -= Time.deltaTime;
|
|
|
|
if (startTimer <= 0f)
|
|
{
|
|
started = true;
|
|
MoveToCurrentPoint();
|
|
}
|
|
|
|
return;
|
|
}
|
|
|
|
if (waiting)
|
|
{
|
|
waitTimer -= Time.deltaTime;
|
|
|
|
if (waitTimer <= 0f)
|
|
{
|
|
waiting = false;
|
|
GoNextPoint();
|
|
}
|
|
|
|
return;
|
|
}
|
|
|
|
if (agent.pathPending)
|
|
{
|
|
return;
|
|
}
|
|
|
|
if (agent.remainingDistance <= arriveDistance)
|
|
{
|
|
StartWait();
|
|
}
|
|
}
|
|
|
|
void MoveToCurrentPoint()
|
|
{
|
|
if (routePoints[currentIndex] == null)
|
|
{
|
|
GoNextPoint();
|
|
return;
|
|
}
|
|
|
|
NavMeshHit hit;
|
|
|
|
if (NavMesh.SamplePosition(routePoints[currentIndex].position, out hit, 2f, NavMesh.AllAreas))
|
|
{
|
|
agent.isStopped = false;
|
|
agent.SetDestination(hit.position);
|
|
}
|
|
else
|
|
{
|
|
Debug.LogWarning(name + " : Route Point°¡ NavMesh ±Ùó¿¡ ¾øÀ½ - " + routePoints[currentIndex].name);
|
|
GoNextPoint();
|
|
}
|
|
}
|
|
|
|
void StartWait()
|
|
{
|
|
waiting = true;
|
|
waitTimer = waitTimeAtPoint;
|
|
agent.isStopped = true;
|
|
}
|
|
|
|
void GoNextPoint()
|
|
{
|
|
if (routePoints.Length <= 1)
|
|
{
|
|
return;
|
|
}
|
|
|
|
if (pingPong)
|
|
{
|
|
if (currentIndex >= routePoints.Length - 1)
|
|
{
|
|
direction = -1;
|
|
}
|
|
else if (currentIndex <= 0)
|
|
{
|
|
direction = 1;
|
|
}
|
|
|
|
currentIndex += direction;
|
|
}
|
|
else
|
|
{
|
|
currentIndex++;
|
|
|
|
if (currentIndex >= routePoints.Length)
|
|
{
|
|
if (loop)
|
|
{
|
|
currentIndex = 0;
|
|
}
|
|
else
|
|
{
|
|
agent.isStopped = true;
|
|
enabled = false;
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
|
|
MoveToCurrentPoint();
|
|
}
|
|
|
|
void OnDrawGizmos()
|
|
{
|
|
if (routePoints == null || routePoints.Length < 2)
|
|
{
|
|
return;
|
|
}
|
|
|
|
Gizmos.color = Color.yellow;
|
|
|
|
for (int i = 0; i < routePoints.Length - 1; i++)
|
|
{
|
|
if (routePoints[i] != null && routePoints[i + 1] != null)
|
|
{
|
|
Gizmos.DrawLine(routePoints[i].position, routePoints[i + 1].position);
|
|
}
|
|
}
|
|
|
|
if (!pingPong && loop)
|
|
{
|
|
if (routePoints[0] != null && routePoints[routePoints.Length - 1] != null)
|
|
{
|
|
Gizmos.DrawLine(routePoints[routePoints.Length - 1].position, routePoints[0].position);
|
|
}
|
|
}
|
|
}
|
|
} |