【Unity】【C#】《U3d人工智能编程精粹》学习心得--------操纵行为--抵达(进入减速范围)

增加Steering的派生类

---SteeringForArrive   抵达行为类

抵达减速范围解读图:

【Unity】【C#】《U3d人工智能编程精粹》学习心得--------操纵行为--抵达(进入减速范围)_第1张图片

案例代码:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SteeringForArrive : Steering {

    public GameObject target;
    private Vehicle m_vehicle;
    private Vector3 desiredVelocity;
    private float maxSpeed;

    public bool isPlanar = true;
    public float arrivalDistance = 0.3f; 
    public float characterRadius = 1.2f;

    public float slowDownDistance;

    private void Start()
    {
        m_vehicle = GetComponent();
        maxSpeed = m_vehicle.maxSpeed;
        isPlanar = m_vehicle.isPlanar;

    }

    public override Vector3 Force()
    {
        //到目标的距离矢量
        Vector3 toTarget = target.transform.position-transform.position;

        //预期速度
        Vector3 desiredVelocity;
        //返回操控向量
        Vector3 returnForce;

        if (isPlanar)
            toTarget.y = 0;
        float distance = toTarget.magnitude;

        if (distance > slowDownDistance)
        {
            desiredVelocity = toTarget.normalized * maxSpeed;
            returnForce = desiredVelocity - m_vehicle.velocity;
        }
        else
        {
            desiredVelocity = toTarget - m_vehicle.velocity;
            returnForce = desiredVelocity - m_vehicle.velocity;
        }
        return returnForce;
    }

    private void OnDrawGizmos()
    {
        Gizmos.DrawWireSphere(target.transform.position, slowDownDistance);
    }

}

参考书籍:《unity3d人工智能编程精粹》 王洪源等著

你可能感兴趣的:(【C#】,【Unity】)