问小白 wenxiaobai
资讯
历史
科技
环境与自然
成长
游戏
财经
文学与艺术
美食
健康
家居
文化
情感
汽车
三农
军事
旅行
运动
教育
生活
星座命理

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类的位置计算逻辑如下:

  1. 确定每一帧的操控力(不超过max_force)
  2. 除以质量得到加速度
  3. 将加速度与原速度相加(不超过max_speed)
  4. 根据速度和时间计算位置变化
  5. 与原位置相加得到新位置

以下是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

实例应用:追逐行为

让我们通过一个简单的追逐行为示例,展示如何组合使用这些类:

  1. 创建一个AI角色和一个目标对象
  2. 为AI角色添加AILocomotion组件
  3. 为AI角色添加Pursuit组件,并设置目标对象
  4. 运行场景,观察AI角色追逐目标的行为

通过这个示例,你可以看到Vehicle类、AILocomotion类和Steering类如何协同工作,实现复杂的AI角色行为。

06

总结与展望

通过本文的学习,你已经掌握了Unity AI角色行为编程的基础知识。从Vehicle类的抽象,到AILocomotion类的移动控制,再到Steering类的具体行为实现,这些核心概念和代码示例将帮助你开发出更智能、更真实的AI角色。

未来,你可以进一步探索群体行为、路径规划等高级AI技术,为你的游戏世界增添更多生动的AI角色。

© 2023 北京元石科技有限公司 ◎ 京公网安备 11010802042949号