自动行为操控Steering(八)—路径跟随(单体操控)

图示:



实现:

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;
        }
    }



  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值