60 lines
1.5 KiB
C#
60 lines
1.5 KiB
C#
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
public class FollowPath : MonoBehaviour
|
|
{
|
|
public enum MovementTypes
|
|
{
|
|
Moveing, Lerping
|
|
|
|
}
|
|
|
|
public MovementTypes MovementType = MovementTypes.Moveing;
|
|
public MovementPath MyPath;
|
|
public float speed = 1;
|
|
public float maxDistance = .1f;
|
|
|
|
public IEnumerator<Transform> pointInPath;
|
|
|
|
// Start is called before the first frame update
|
|
void Start()
|
|
{
|
|
if (MyPath == null)
|
|
{
|
|
Debug.Log("path not attached");
|
|
return;
|
|
}
|
|
pointInPath = MyPath.GetNextPathPoint();
|
|
pointInPath.MoveNext();
|
|
if (pointInPath.Current == null)
|
|
{
|
|
Debug.Log("need points");
|
|
return;
|
|
}
|
|
transform.position = pointInPath.Current.position;
|
|
}
|
|
|
|
// Update is called once per frame
|
|
void Update()
|
|
{
|
|
if (pointInPath == null || pointInPath.Current == null)
|
|
{
|
|
return;
|
|
}
|
|
if (MovementType == MovementTypes.Moveing)
|
|
{
|
|
transform.position = Vector3.MoveTowards(transform.position, pointInPath.Current.position, Time.deltaTime * speed);
|
|
}
|
|
else
|
|
{
|
|
transform.position = Vector3.Lerp(transform.position, pointInPath.Current.position, Time.deltaTime * speed);
|
|
}
|
|
|
|
var distanceSqare = (transform.position - pointInPath.Current.position).sqrMagnitude;
|
|
if (distanceSqare < maxDistance * maxDistance)
|
|
{
|
|
pointInPath.MoveNext();
|
|
}
|
|
}
|
|
}
|