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

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


1,交通工具类,在AI架构模型中,通常把AI抽象为一个点,一般包含position,mass,velocity等等的信息。

而速度随着施加的力而变化,因此还要有maxSpeed和maxForce来进行限制。

using UnityEngine;
using System.Collections;

public class Vehicle : MonoBehaviour {

    //这个AI角色包含的操纵行为列表
    private Steering[] steerings;
    
    public float maxSpeed = 10;

    public float maxForce = 100;

    protected float sqrMaxSpeed;
    //角色质量
    public float mass = 1;

    public Vector3 velocity;
    //转向速度
    public float damping = 0.9f;
    //间隔时间
    public float computeInterval = 0.2f;

    public bool isPlanar = true;

    protected Vector3 steeringForce;

    protected Vector3 acceleration;

    protected float timer;

    protected void Start()
    {
        steeringForce = Vector3.zero;

        sqrMaxSpeed = maxSpeed * maxSpeed;

        timer = 0;

        steerings = GetComponents<Steering>();
    }

    void Update()
    {
        timer += Time.deltaTime;

        steeringForce = Vector3.zero;

        if(timer >computeInterval )
        {
            foreach (Steering  s in steerings )
            {
                if(s.enabled )
                {
                    steeringForce += s.Force() * s.weight;
                }
            }

            steeringForce = Vector3.ClampMagnitude(steeringForce, maxForce);

            acceleration = steeringForce / mass;

            timer = 0;
        }
    }
}


2,真正操纵AI角色移动的类,包括计算每次移动的距离,动画等等。它派生自Vehicle

using UnityEngine;
using System.Collections;

public class AILocomotion : Vehicle {

    private CharacterController controller;

    private Rigidbody theRigidbody;

    private Vector3 moveDistance;

    void Start()
    {
        controller = GetComponent<CharacterController>();

        theRigidbody = GetComponent<Rigidbody>();

        moveDistance = Vector3.zero;

        base.Start();
    }

    void FixedUpdate()
    {
        velocity += acceleration * Time.fixedDeltaTime;

        if(velocity .sqrMagnitude >sqrMaxSpeed )
        {
            velocity = velocity.normalized * maxSpeed;
        }

        moveDistance = velocity * Time.fixedDeltaTime;

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

        if(controller !=null )
        {
            controller.SimpleMove(velocity);
        }
        else if(theRigidbody ==null ||theRigidbody .isKinematic )
        {
            transform.position += velocity;
        }
        else
        {
            theRigidbody.MovePosition(theRigidbody.position + moveDistance);
        }

        if(velocity .sqrMagnitude >0.00001)
        {
            Vector3 newForward = Vector3.Slerp(transform.forward, velocity, damping * Time.deltaTime);
            if(isPlanar )
            {
                newForward.y = 0;

                transform.forward = newForward;
            }
        }
        //播放动画等等操纵
    }

}


3,各种操作行为的基类,其中的Force()由它的派生类实现

using UnityEngine;
using System.Collections;

public class Steering : MonoBehaviour {

    public float weight = 1;

    void Start()
    {

    }
    void Update()
    {

    }

    public virtual Vector3 Force()
    {
        return Vector3.zero;
    }
}


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

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


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
unity游戏人工智能AI系统框架Eliot - AI fr amework 1.2.1 所支持的Unity版本:5.6.0 及以上版本 Eliot借助可视化编程和算法库,可以在几分钟内创建高质量的AI,这些算法可以为您的高质量的AI角色提供您在游戏中可能需要的所有类型的交互操作。 Eliot 是一套让您专注于在游戏中设计原型并制作角色(AI),尽可能减少分心的工具。它专注于更快地实现目的! 是什么让Eliot与众不同?是设计原型和搭建AI的超快速度,是能让您专注于‘什么你想创建的’,而不是‘该如何创建它’的出众能力。Eliot 是怎样实现的?通过独特的功能集,包括内置的可视化脚本处理,技能,路径系统(waypoints) 和丰富的角色身体模拟系统。 特点 可视化脚本处理:在几分钟内构建复杂的行为。利用可视化脚本的强大功能,可以在人类语言级别上编辑行为 感知:Eliot角色使用射线来感知他们周围的世界,能够支持Stealth风格以及经典RPG风格的感知。 动作:角色可以通过使用Unity NavMesh,A * Pathfinding Project Pro或者是您自定义的扩展插件所构建的系统移动。这个系统拥有一个算法库,能随时为您提供游戏中可能需要的任何类型的动作,例如步行,跑步,躲避,巡逻等。还包括炮塔模式。 仓库: 角色可以提取,使用,拿起和丢弃物品。物品包括武器,药水,硬币,资源等。 资源:角色生存时,使用能量进行活动。角色在系统中可以使用资源(例如生命值和能量值)和其他角色完成交互,也可以在与‘世界’的交互中增加或减少资源值。 技能:攻击,治疗,施放法术......任何您想要的技能。已把全类型技能的的交互封装在一个文件内,方便您以后在必要或需要时按名称调用它。 路径系统(waypoint): 创建、连接方向点,形成并跟随路径,定义区域。Eliot的路径系统允许您通过点击画面来创建路径。您可以使用它在编辑器的已定义区域内以及在游戏模式下生成预制件,或者产生一条能够在达到某一位置后改变角色行为的路径。 优化:Eliot可以处理大量需求。Eliot角色具有多个属性,可让您在必要时通过简单地更改属性值来优化性能。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值