Unity AI角色行为编程入门:从基础类到复杂行为
创作时间:
作者:
@小白创作中心
Unity AI角色行为编程入门:从基础类到复杂行为
引用
CSDN
等
10
来源
1.
https://blog.csdn.net/dmk17771552304/article/details/113951925
2.
https://blog.csdn.net/m0_63024355/article/details/133869254
3.
https://blog.csdn.net/qq_43293994/article/details/112761256
4.
https://blog.csdn.net/dmk17771552304/article/details/113951925#t2
5.
https://blog.csdn.net/dmk17771552304/article/details/113951925#t1
6.
https://blog.csdn.net/qq_41727685/article/details/124419820
7.
https://docs.unity3d.com/cn/2019.3/Manual/com.unity.modules.vehicles.html
8.
https://docs.unity3d.com/cn/2021.1/Manual/com.unity.modules.vehicles.html
9.
https://cloud.tencent.com/developer/article/1636853
10.
https://www.lfzxb.top/context-steering-behavior-driven-steering-at-the-macro-scale/
在游戏开发中,AI角色的行为是提升游戏沉浸感和可玩性的重要因素。而实现这些行为的核心技术,就是AI角色行为编程。本文将带你从零开始,学习如何在Unity中通过Vehicle类、AILocomotion类和Steering类来实现AI角色的自主移动。
01
基础概念
在Unity AI角色行为编程中,有三个核心类:Vehicle类、AILocomotion类和Steering类。它们共同构成了AI角色移动的基础框架。
- Vehicle类:将AI角色抽象为一个质点,包含位置、速度、质量等信息。
- AILocomotion类:继承自Vehicle类,负责控制AI角色的实际移动。
- Steering类:实现各种具体的行为,如追逐、逃避、徘徊等。
02
Vehicle类详解
Vehicle类是AI角色行为编程的基础。它将AI角色抽象为一个质点,包含以下关键属性:
- 位置(position):角色在游戏世界中的当前位置。
- 速度(velocity):角色的当前移动速度。
- 质量(mass):角色的质量,影响加速度。
- 最大速度(max_speed):角色能达到的最大速度。
- 最大力(max_force):能施加到角色身上的最大力。
- 朝向(orientation):角色的朝向信息。
Vehicle类的位置计算逻辑如下:
- 确定每一帧的操控力(不超过max_force)
- 除以质量得到加速度
- 将加速度与原速度相加(不超过max_speed)
- 根据速度和时间计算位置变化
- 与原位置相加得到新位置
以下是Vehicle类的代码实现:
using UnityEngine;
namespace AI
{
public class Vehicle : MonoBehaviour
{
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 IsPlannar = true;
private Vector3 _steeringForce;
protected Vector3 _acceleration;
private float _timer;
protected void OnStart()
{
_steeringForce = Vector3.zero;
_sqrMaxSpeed = MaxSpeed * MaxSpeed;
_timer = 0;
_steerings = GetComponents<Steering>();
}
private 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;
}
}
}
}
03
AILocomotion类详解
AILocomotion类继承自Vehicle类,负责实现AI角色的实际移动。它包含了CharacterController和Rigidbody组件,用于处理物理交互和动画播放。
以下是AILocomotion类的关键代码:
using UnityEngine;
public class AILocomotion : Vehicle
{
private CharacterController controller;
private Rigidbody theRigidbody;
private Vector3 moveDistance;
void Start()
{
controller = GetComponent<CharacterController>();
theRigidbody = GetComponent<Rigidbody>();
moveDistance = new Vector3(0, 0, 0);
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;
}
controller.Move(moveDistance);
theRigidbody.MovePosition(transform.position + moveDistance);
}
}
04
Steering类应用
Steering类用于实现具体的操控行为。例如,追逐行为(Pursuit)和逃避行为(Evade)可以通过以下方式实现:
public class Pursuit : Steering
{
public Transform target;
public float predictionTime = 1.0f;
public override Vector3 Force()
{
Vector3 targetPos = target.position + target.velocity * predictionTime;
return targetPos - transform.position;
}
}
public class Evade : Steering
{
public Transform target;
public float predictionTime = 1.0f;
public override Vector3 Force()
{
Vector3 targetPos = target.position + target.velocity * predictionTime;
return transform.position - targetPos;
}
}
05
实例应用:追逐行为
让我们通过一个简单的追逐行为示例,展示如何组合使用这些类:
- 创建一个AI角色和一个目标对象
- 为AI角色添加AILocomotion组件
- 为AI角色添加Pursuit组件,并设置目标对象
- 运行场景,观察AI角色追逐目标的行为
通过这个示例,你可以看到Vehicle类、AILocomotion类和Steering类如何协同工作,实现复杂的AI角色行为。
06
总结与展望
通过本文的学习,你已经掌握了Unity AI角色行为编程的基础知识。从Vehicle类的抽象,到AILocomotion类的移动控制,再到Steering类的具体行为实现,这些核心概念和代码示例将帮助你开发出更智能、更真实的AI角色。
未来,你可以进一步探索群体行为、路径规划等高级AI技术,为你的游戏世界增添更多生动的AI角色。
热门推荐
多因素认证(MFA):构建多重防御的身份验证方法
晚期肝癌治疗迎来新希望:BB102临床试验展现显著疗效
涉江采芙蓉原文及翻译 原文赏析
涉江采芙蓉,兰泽多芳草。
人工智能的应用成果
应急方案和应急预案的区别是什么
婴儿接受心脏彩超检查需要使用的镇静药物
点痣去大医院还是整形医院?如何选择更适合自己的医疗机构?
首乌藤和夜交藤一样吗?一文详解这两种中药材
首乌藤和夜交藤一样吗?医生专业解答来了
孩子总求陪伴?家长如何满足孩子情感需求的秘诀
电脑任务栏无响应怎么办?多种实用解决方案帮你轻松应对
工商银行A股股息率超过7%,究竟发生了什么?
中风的中医辨证分型与病机
春季眼睛痒怎么办?3种有效方法帮你快速缓解不适
汽车保养与维护全攻略:从机油到内饰的全方位指南
如何管理项目前期费用
红外相机,带你看见肉眼看不见的世界
电赛必备——PID控制算法详解
人工智能如何识别方言
全球影史票房前十电影大盘点
看病买药不带卡,就用医保码!这份医保码使用全攻略请收藏
驾驶摩托车、电动自行车交通安全知识指南,请查收!
电子医保卡的适用范围及使用指南
转角衣柜门铰链安装指南:从选购到维护全攻略
放大电路的分析方法
多级放大电路的频率响应分析
《指环王》中的英雄之旅:主要角色的成长、发展与转变分析
游戏搬砖工作室案件的法律分析与探讨
中国无梁殿建筑特色