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角色。
热门推荐
儋州必打卡:东坡书院、龙门激浪、白马井古迹
我国核桃产业发展现状:面积和产量均居首位
关于川哥的搞笑视频合集
4剧场108情境空间,王潮歌打造沉浸式“红楼梦戏剧幻城”
“只有红楼梦·戏剧幻城”:王潮歌打造的10亿沉浸式文旅新地标
广电卡正式商用:中国广电以700MHz频段布局移动通信
5G时代4G信号变差?四大原因及优化方案全解析
移动网络覆盖最广,广电信号待验证:四大运营商网络对比
冬季电暖器故障排查:13种常见问题及维修方法
电暖桌安装与故障处理:8步安装+4类故障详解
儋州老市村盐田生态修复:实现生态、经济、文化的“一石三鸟”
探访东坡书院,揭秘儋州最美自然景观
个人借条欠款单定制指南:从标题到履行的全流程详解
青州古城四季游:春赏樱、夏避暑、秋观田、冬品年
民事诉讼证据排除规则:四大维度保障司法公正
辽宁奉国寺:立法保护加活化利用,游客量增4倍
奉国寺:1800平方米大雄殿与4000平方米辽代彩绘同为国宝
应县木塔见证辽代文明:建筑、瓷器与墓葬艺术全览
如何挑选环保又耐用的石膏板?这些选购要点请收好
中国石膏行业产量连续增长,2023年突破14.96亿吨创新高
黑青玉的神明供奉价值与意义:一种神秘的信仰符号?
土地公:从远古农耕文化走来的民间守护神
为什么家家户户都要供奉土地公
M390高速钢:化学成分、性能及应用全解析
家庭风水中的四柱八字:揭秘传统智慧的运用与实践
高速钢和钨钢的区别是什么?
黄晓明《戴假发的人》:420万票房背后的转型之痛
黄晓明新片遇冷,首映礼鞠躬道歉能否挽回口碑?
黄晓明东京电影节红毯造型惹争议,假发背后另有深意
广中珠澳高铁若调整路线,不设中山南站将对中山交通有什么影响?