图示:
实现:
public enum PatrolMode
{
Once,
Loop,
PingPong,
}
///<summary>
///路径跟随
///</summary>
public class FollowToPathSteering : Steering
{
/// <summary>巡逻到达的距离 </summary>
public float patrolArrivalDistance = 1;
/// <summary>路点 </summary>
public Transform[] wayPoints;
/// <summary>巡逻模式 </summary>
public PatrolMode partrolMode = PatrolMode.Once;
private int currentWayPoint;
private bool IsPatrolComplete;
public override Vector3 ComputerFinalForce()
{
exceptForce = (wayPoints[currentWayPoint].position - transform.position).normalized * speed;
//是否到达当前路点
if (Vector3.Distance(transform.position, wayPoints[currentWayPoint].position) < patrolArrivalDistance)
{
//是否是最后一个路点
if (currentWayPoint == wayPoints.Length - 1)
{
//多分支
switch (partrolMode)
{
//单次:
case PatrolMode.Once:
IsPatrolComplete = true;
return Vector3.zero;
//往返:
case PatrolMode.PingPong:
Array.Reverse(wayPoints);
break;
}
}
currentWayPoint = (currentWayPoint + 1) % wayPoints.Length;
}
//移动:
//MoveToTarget(wayPoints[currentWayPoint].position, walkSpeed, patrolArrivalDistance);
//动画:
//fsm.PlayAnimation(fsm.animaParm.Walk);
return (exceptForce - vehicle.currentForce) * weight;
}
}