【Unity】【C#】《U3d人工智能编程精粹》学习心得--------操纵行为--路径跟随

增加Steering的派生类

----SteeringForFollowPath类    路径跟随行为类

路径跟随行为类解读:

行为准备:

①记录全部路径点,统计路径点数。

②获取下一个路径点,并获取当前是几号路径点。(用来标志是否为终点)

准备是否到达目标点的条件:

①与路径点的距离小于某个值,判断为已经到达路点  arriveDistance   sqrArriveDistance

通过判断是否到达进行切换下一个路径点

通过判断是否是否到达最终点来判断是否执行靠近行为抵达行为

产生疑问:

Vector3.magnitude  与 Vector3.sqrMagnitude 的运算速度问题

在官方文档说用SqrMagnitude比较速度会比较快.

可是我在Unity中测试这两个方法,花费时间还是相同的。

(没空测试,这里留白,一切还是按官方文档的)

测试代码如下:

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

public class TestSqr : MonoBehaviour {
    public Vector3 v3 = new Vector3(1, 2, 3);
	// Use this for initialization
	void Start () {
        Stopwatch sw = new Stopwatch();
        sw.Start();
        //for(int i = 0; i < 1000000000; i++)
        //{
        //    float sqr = v3.sqrMagnitude;
        //}
        sw.Stop();
        TimeSpan time2 = sw.Elapsed;
        print(time2);

        Stopwatch sw2 = new Stopwatch();
        sw2.Start();
        //for (int i = 0; i < 1000000000; i++)
        //{
        //    float v = v3.magnitude;
        //}
        sw2.Stop();
        TimeSpan time3 = sw.Elapsed;
        print(time3);
    }
}

案例代码:

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

public class SteeringForFollowPath : Steering {
    //节点数组来表示路径
    public GameObject[] waypoints;
    //目标点
    private Transform target;
    //当前路点
    private int currentNode;
    //与路点的距离小于这个值时,认为已经到达,可以向下一个路点触发
    private float arriveDistance;
    private float sqrArriveDistance;
    //路点的数量
    private int numberOfNodes;
    //操控力
    private Vector3 force;
    //预期速度
    private Vector3 desiredVelocity;
    private Vehicle m_vehicle;
    private float maxSpeed;
    private bool isPlanar;
    //当与目标小于这个距离时,开始减速
    public float slowDownDistance;


    // Use this for initialization
	void Start () {
        numberOfNodes = waypoints.Length;
        m_vehicle = GetComponent<Vehicle>();
        maxSpeed = m_vehicle.maxSpeed;
        isPlanar = m_vehicle.isPlanar;
        //设置当前路点为第0个路点
        currentNode = 0;
        //设置当前路点为目标点
        target = waypoints[currentNode].transform;
        arriveDistance = 1.0f;
        sqrArriveDistance = arriveDistance * arriveDistance;
	}
	
	public override Vector3 Force()
    {
        force = new Vector3(0, 0, 0);
        Vector3 dist = target.position - transform.position;
        if (isPlanar)
            dist.y = 0;
        //如果当前路点已经是最后一个路点了
        if (currentNode == numberOfNodes - 1)
        {
            //如果与当前路点的距离大于减速距离
            if (dist.magnitude > slowDownDistance)
            {
                //求出预期速度
                desiredVelocity = dist.normalized * maxSpeed;
                //计算操控向量
                force = desiredVelocity - m_vehicle.velocity;

            }
            else
            {
                //与当前路点距离小于减速距离,开始减速,计算操控向量
                desiredVelocity = dist - m_vehicle.velocity;
                force = desiredVelocity - m_vehicle.velocity;

            }
        }
        else
        {
            //不是最后一个路点
            if (dist.sqrMagnitude < sqrArriveDistance)
            {
                //如果与当前路点距离的平方小于到达距离的平方
                //可以开始靠近下一个路点,将下一个路点设置为目标点
                currentNode++;
                target = waypoints[currentNode].transform;
            }
            //计算预期速度和操控向量
            desiredVelocity = dist.normalized * maxSpeed;
            force = desiredVelocity - m_vehicle.velocity;
        }
        return force;
    }
}

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

猜你喜欢

转载自blog.csdn.net/Terrell21/article/details/82454464