【AI】角色的自主移动(1)——基类

直接上代码,《AI人工编程精粹》的代码,方便日后查看

 

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

/// <summary>
/// 基类 基础运动
/// </summary>
public class Vehicle: MonoBehaviour {
    //这个AI角色的操控行为列表
    private Steering[] steerings;

    //设置这个AI角色能达到的最大速度
    public float maxSpeed = 10;

    //设置能施加到这个AI角色的力的最大值
    public float maxForce = 100;

    //最大速度的平方,通过预算出并存储,节省资源
    protected float sqrMaxSpeed;

    //AI角色的质量
    public float mass = 1;

    //AI角色的速度
    public Vector3 velocity;

    //控制转向时的速度;
    public float damping = 0.9f;

    //操控力的计算间隔施加,为了达到更高的帧率,操控力不需要每帧更新
    public float computeInterval = 0.2f;

    //是否在2d平面上
    public bool isPlanar = true;

    //计算得到的操控力
    private Vector3 steeringForce;

    //AI角色的加速度
    protected Vector3 acceleration;

    //计时器
    private float timer;

    protected void Start()
    {
        steeringForce = new Vector3(0, 0, 0);
        sqrMaxSpeed = maxSpeed * maxSpeed;
        timer = 0;

        //获取行为列表
        steerings = GetComponents<Steering>();

    }

    void Update()
    {
        timer += Time.deltaTime;
        steeringForce = new Vector3(0, 0, 0);
        //如果距离上次计算操控力的时间大于设定的时间间隔computeInterval;
        //再次计算操控力

        if (timer>computeInterval)
        {
            foreach (Steering item in steerings)
            {
                if (item.enabled)
                {
                    steeringForce += item.Force() * item.weight;
                }
            }
            //是操控力不大于maxForce
            steeringForce = Vector3.ClampMagnitude(steeringForce, maxForce);

            //力除以质量,求出加速度
            acceleration = steeringForce / mass;

            //重新开始计时
            timer = 0;
        }

    }
}

——————————————————————————————————————————————————————————————————————————————————————————————————————————————————

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

/// <summary>
/// 基类 基础运动
/// </summary>
public class Vehicle: MonoBehaviour {
    //这个AI角色的操控行为列表
    private Steering[] steerings;

    //设置这个AI角色能达到的最大速度
    public float maxSpeed = 10;

    //设置能施加到这个AI角色的力的最大值
    public float maxForce = 100;

    //最大速度的平方,通过预算出并存储,节省资源
    protected float sqrMaxSpeed;

    //AI角色的质量
    public float mass = 1;

    //AI角色的速度
    public Vector3 velocity;

    //控制转向时的速度;
    public float damping = 0.9f;

    //操控力的计算间隔施加,为了达到更高的帧率,操控力不需要每帧更新
    public float computeInterval = 0.2f;

    //是否在2d平面上
    public bool isPlanar = true;

    //计算得到的操控力
    private Vector3 steeringForce;

    //AI角色的加速度
    protected Vector3 acceleration;

    //计时器
    private float timer;

    protected void Start()
    {
        steeringForce = new Vector3(0, 0, 0);
        sqrMaxSpeed = maxSpeed * maxSpeed;
        timer = 0;

        //获取行为列表
        steerings = GetComponents<Steering>();

    }

    void Update()
    {
        timer += Time.deltaTime;
        steeringForce = new Vector3(0, 0, 0);
        //如果距离上次计算操控力的时间大于设定的时间间隔computeInterval;
        //再次计算操控力

        if (timer>computeInterval)
        {
            foreach (Steering item in steerings)
            {
                if (item.enabled)
                {
                    steeringForce += item.Force() * item.weight;
                }
            }
            //是操控力不大于maxForce
            steeringForce = Vector3.ClampMagnitude(steeringForce, maxForce);

            //力除以质量,求出加速度
            acceleration = steeringForce / mass;

            //重新开始计时
            timer = 0;
        }

    }
}

_______________________________________________________________________________________________________________________________________________________________________________________________________

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// 移动类
/// </summary>
public class AILocomotion : Vehicle {

    

    //AI角色的控制器
    private CharacterController controller;

    //AI角色的Rigibody
    private Rigidbody theRigibody;

    //AI每次的移动距离
    private Vector3 moveDistance;

    void Start()
    {
           //获取控制机器
        controller = GetComponent<CharacterController>();

        //获取刚体
        theRigibody = GetComponent<Rigidbody>();

        moveDistance = new Vector3(0, 0, 0);

        //调用基类函数
        base.Start();
    }

    //物理相关操作在FixUpadte更新
    void FixedUpdate()
    {
        //计算速度
        velocity += acceleration * Time.fixedDeltaTime;
        //限制速度
        if (velocity.sqrMagnitude>sqrMaxSpeed)
        {
            velocity = velocity.normalized * maxSpeed;
        }
        //计算AI角色的移动
        moveDistance = velocity * Time.fixedDeltaTime;

        if (isPlanar)
        {
            velocity.y = 0;
            moveDistance.y = 0;
        }


        if (controller!=null)
        {
            controller.SimpleMove(velocity);

        }
        else if (theRigibody==null|| theRigibody.isKinematic)
        {
            transform.position += moveDistance;
        }
        else
        {
            theRigibody.MovePosition(theRigibody.position + moveDistance);
        }

        if (velocity.sqrMagnitude>0.0001)
        {
            //通过当前朝向与速度方向的差值,计算新的朝向。
            Vector3 newForward = Vector3.Slerp(transform.forward, velocity, damping * Time.deltaTime);

            if (isPlanar)
            {
                newForward.y = 0;
                transform.forward = newForward;
            }
        }

        //播放动画
    }
}

 

posted @ 2017-08-24 09:59  2z1h5  阅读(384)  评论(0编辑  收藏  举报