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角色。
热门推荐
复旦团队重大突破登Cell,破纪录复活“冰封”18个月人脑类器官
Excel随机生成数字怎么设置
分手七天后,女友的一条朋友圈,挽回了我们四年的感情
为什么金星离太阳这么近,自转却很慢?
AI驱动,打造未来金融支付新体验
1月逾百亿美元外资净流入,DeepSeek概念带动机构重估中国市场价值
蒙古族舞蹈:草原上流淌的力与美
高考期间衣服怎么穿?不要穿这3类,关乎孩子成绩,你别不以为然
四川黄龙旅游住宿全指南:不同预算与需求下的更佳住宿选择
抗结直肠癌的10种中药
多家快递擅自送货不上门,涉及“三通一达”、极兔等
2025年赛博玄学生存指南:解锁当代年轻人的数字求签大法
情商不高怎么办,别怕,这些建议让你情商飙升
青岛地铁免费乘车政策详解:这些人群可享受优惠
黑白灰现代简约风格装修效果图, 设计师揭秘超绝布局!
肿瘤专家马晓东:垂体瘤治疗大揭秘,带你走出疾病阴霾
截瘫大小便失禁的恢复方法
“吸引”男人,让他更爱你的方式:一种行为
不同颜色丁香花的花语
AI正在触发最剧烈的职业重构,知识加速折旧,“价值导演”将成为职场新物种
13 个网站 URL 优化小技巧,创建 SEO 友好的网址
股票涨了几个点通常意味着什么?
浙江十大年糕做法,每一种都让人垂涎三尺!
跨文化理解的深化:引导孩子尊重并欣赏多元文化背景
电动汽车保值率和售后维修成本的关系
关于电影191:《无间道》和《无间道风云》
“考古中国”重大发现!江苏盐城盐业、淮安运河考古新成果发布
如何将中文标点符号批量替换成英文标点符号
以暴制暴!8部最好看的黑帮美剧,第一公认的神作!
彻底搞懂它!一文带你掌握函数定义域的秘密武器