Artificial Intelligence with Unity: Introducing Intelligent Behaviour to Game Characters

Artificial Intelligence with Unity

Hello everyone, today I wanted to research something on Unity and I came across artificial intelligence almost everywhere. Because artificial intelligence (AI) in the game development world is like a magical world! AI gives realistic behaviours to game characters, making the game world feel more dynamic and intelligent. Think about it, in a game you are playing, if enemies make strategic moves or NPCs exhibit environmentally sensitive behaviours, how much it strengthens the atmosphere of the game, right?

In this article, we will talk about how you can give intelligent behaviours to game characters using Unity. In other words, making your enemy characters feel like real opponents instead of just bricks!

1. Let’s Meet Artificial Intelligence

Firstly, what is artificial intelligence? Actually, what we call game AI is to ensure that the characters act according to certain rules and algorithms. The aim is to create an opponent or friend that is more dynamic and reacts according to the situation, rather than a character that only exhibits scripted behaviour.

The first step in developing artificial intelligence in Unity is to learn pathfinding and movement systems. Here we come across NavMesh.

2. NavMesh: Let’s Draw Paths for Our Characters!

One of the most common ways to determine how characters move through the environment in Unity is to use NavMesh (Navigation Mesh). NavMesh defines paths and areas where characters can move. Once you have created this, you have solved the question of how your character will reach a certain point. Let’s look at this in a little more detail:

What is NavMesh?

NavMesh is a virtual network that defines navigable areas in the game world. This network determines the paths and obstacles along which characters can move. For example, it includes floors, walls, and other obstacles where characters can walk. NavMesh is like a map that allows characters to move in a natural and logical way.

How to Create NavMesh?

To create a NavMesh, you must follow these steps:

  1. In the Unity Editor, open the Navigation window by clicking AI and then Navigation from the Window menu.
  2. To specify which areas of the floor and other surfaces in your game to create NavMesh on, mark these areas as ‘Navigation Static’. This ensures that these surfaces are taken into account when creating the NavMesh.
  3. In the Navigation window, go to the Bake tab and create the NavMesh by clicking the Bake button. Unity analyses the navigable areas in the scene and converts them into NavMesh.

NavMesh Agent Component

To make your characters move on the NavMesh, you need to add the NavMesh Agent component to each character. This component controls the character’s movement on the NavMesh.

Add the NavMesh Agent component to your character and set the component’s properties (speed, rotation speed, acceleration, etc.). These settings determine how your character moves on the NavMesh.

Movement and Goal Setting

To make your character reach a specific point, you can set destination points using the NavMesh Agent component:

  • Setting a Destination by Code: You can use the NavMesh Agent component to make the character move to a target point. For example, to make an enemy character follow the player, you can set the player’s position as the destination.
using UnityEngine;
using UnityEngine.AI;

public class EnemyAI : MonoBehaviour
{
public Transform player; // Player Object
private NavMeshAgent agent;
void Start()
{
agent = GetComponent<NavMeshAgent>();
}
void Update()
{
if (player != null)
{
agent.SetDestination(player.position); // Direct the enemy to the player's position
}
}
}
  • Reaching Destinations: When your character moves on NavMesh, the NavMesh Agent component automatically calculates the character’s path and ensures that he reaches the destination by the shortest route.

Barriers and Dynamic Changes

In some cases, there may be dynamic changes in the scene (for example, moving obstacles). In such cases, NavMesh needs to react to these changes. You can use the NavMesh Obstacle component to do this. This component specifies how obstacles in the scene affect NavMesh. It allows obstacles to have an effect on the NavMesh and allows characters to overcome or go around them when necessary.

3. Time for Decision Making: Behaviour Trees

You’ve got your characters moving, great! Now it’s time for them to make decisions. You can use behaviour trees or finite state machines (FSM) to determine how your game characters react to certain situations.

Behaviour trees are an effective way to model decision-making processes. For example, when an enemy character sees your player, will it decide to attack or run away? You can model such decisions as branching trees. You can also find great add-ons in the Unity Asset Store that facilitate this functionality.

The FSM provides a structure that represents a character’s states. Your character’s state can change from ‘patrolling’ to ‘attacking’ if they recognise danger. This structure allows for more organised and manageable character behaviour.

4. Friend or Enemy? Let’s Categorise Our Characters

There are different types of AI in each game: Enemies, friends and independent NPCs. You will need to develop different behaviours for each of them. For example, your enemy AI will attack the player, while your friendly AI character will perhaps try to help them. Developing different strategies for each AI type makes your job easier and your characters more interesting.

5. Smarter AI with Machine Learning: Unity ML-Agents

If you want to go even deeper into AI, you can train your characters using machine learning (ML) with Unity ML-Agents. Unity offers tools that facilitate AI training in your games with ML-Agents. With ML-Agents, your characters can have the ability to learn in-game. Rather than pre-coding your characters’ behaviour, this allows them to make decisions on their own in certain situations.

For example, your enemies can learn the player’s strategies over time and develop more complex attack strategies. This can make the game experience much more dynamic and challenging.

6. Inspirations from the Real World!

There are many games that develop AI using Unity. For example, Alien: Isolation, for example, managed to give the player a constant sense of danger by using artificial intelligence in an impressive way. The strategic movement of your enemies on the playing field and their challenge to the player will take your game to the next level.

In conclusion, developing AI with Unity seems to be the key to providing your players with a more realistic and richer experience, given recent developments. From simple NavMesh implementations to complex AI behaviour trees and even machine learning, there is a wide range of AI development methods available, and this is truly a vast subject. If you want the characters in your games to be ‘smarter’, you should start learning the tricks of AI development.

Happy coding to everyone.

Selin.

 

Hiç yorum yok: