unity3d AI 学习--个体行为操控(1)--具体行为类(2)

摘自《Unity3D人工智能编程精粹》


以下都是继承Steering类的,直接上代码了:


Pursuit(追逐):

using UnityEngine;
using System.Collections;

public class SteeringForPursuit : Steering  {

    public GameObject target;

    private Vector3 desiredVelocity;

    private Vehicle _vehicle;

    private float maxSpeed;

    void Start()
    {
        _vehicle = GetComponent();
        maxSpeed = _vehicle.maxSpeed;
    }

    public override Vector3 Force()
    {
        Vector3 toTarget = target.transform.position - transform.position;
        //计算追逐着的前向与逃避者前向的夹角
        float relativeDirection = Vector3.Dot(transform.forward, target.transform.forward);
        //如果夹角大于0,并且追逐者朝向逃避者,就直接跑向逃避者
        if(Vector3 .Dot (toTarget ,transform .forward )>0&&(relativeDirection <-0.95f))
        {
            //计算预期的速度
            desiredVelocity = (target.transform.position - transform.position).normalized * maxSpeed;
            return (desiredVelocity - _vehicle.velocity);
        }

        //计算预测时间,正比于追逐者与逃避者的距离,反比于追逐者和逃避者的速度和
        float lookAheadTime = toTarget.magnitude / (maxSpeed +
            target.GetComponent().velocity.magnitude);
        //计算预期速度
        desiredVelocity = (target.transform.position + 
            target.GetComponent().velocity 
            *lookAheadTime - transform.position).normalized * maxSpeed;

        return (desiredVelocity - _vehicle.velocity);
    }
}


Evade(逃避):


using UnityEngine;
using System.Collections;

public class SteeringForEvade : Steering  {
    public GameObject target;

    private Vector3 desiredVelocity;

    private Vehicle _vehicle;

    private float maxSpeed;

    void Start()
    {
        _vehicle = GetComponent();
        maxSpeed = _vehicle.maxSpeed;
    }

    public override Vector3 Force()
    {
        Vector3 toTarget = target.transform.position - transform.position;
        //向前预测的时间
        float lookAheadTimer = toTarget.magnitude / (maxSpeed +
            target.GetComponent().velocity.magnitude);

        desiredVelocity = (transform.position - (target.transform.position +
            target.GetComponent().velocity * lookAheadTimer)).normalized * maxSpeed;

        return (desiredVelocity - _vehicle.velocity);
    }
}



Wander(徘徊或者说巡逻):


using UnityEngine;
using System.Collections;

public class SteeringForWander : Steering {
    //徘徊的半径
    public float wanderRadius;
    //徘徊的距离,即wander圈凸出在AI角色前面的距离
    public float wanderDistance;
    //每秒加到目标的随机位移的最大值
    public float wanderJitter;

    public bool isPlanar;

    private Vector3 desiredVelocity;

    private Vehicle _vehicle;

    private float maxSpeed;

    private Vector3 circleTarget;

    private Vector3 wanderTarget;

    void Start()
    {
        _vehicle = GetComponent();
        maxSpeed = _vehicle.maxSpeed;
        isPlanar = _vehicle.isPlanar;
        //随机初始点
        circleTarget = new Vector3(wanderRadius * 0.707f, 0, wanderRadius * 0.707f);
    }

    public override Vector3 Force()
    {
        //计算随机位移
        Vector3 randomDisplacement = new Vector3((Random.value - 0.5f) *
            2 * wanderJitter, (Random.value - 0.5f) * 2 * wanderJitter,
            (Random.value - 0.5f) * 2 * wanderJitter);

        if(isPlanar )
        {
            randomDisplacement.y = 0;
        }
        //计算新的位置
        circleTarget += randomDisplacement;
        //由于新的位置可能不在圆周上,因此需要投影到圆周上
        circleTarget = wanderRadius * circleTarget.normalized;
        //转换为世界坐标
        wanderTarget = _vehicle.velocity.normalized * wanderDistance + 
            circleTarget + transform.position;
        //计算预期速度
        desiredVelocity = (wanderTarget - transform.position).normalized * maxSpeed;

        return (desiredVelocity - _vehicle.velocity);
    }
}


原谅我作为原创发表,这本书的确很好,比我之前看到的一个外国大神写的《unity3d人工智能编程》更加详细和全面,我会每天发表一篇博客,作为学习记录的.......

人笨,跟着各路大神学习有汤喝就足够了



你可能感兴趣的:(Unity3d,AI)