In this section, we will explore the fundamentals of AI (Artificial Intelligence) scripting in Unity. We will cover basic concepts and provide practical examples to help you create simple AI behaviors for your game characters.

Key Concepts

  1. AI Basics:

    • Understanding AI in games.
    • Common AI behaviors (e.g., patrolling, chasing, fleeing).
  2. NavMesh:

    • Introduction to Unity's Navigation Mesh (NavMesh).
    • Setting up a NavMesh for AI navigation.
  3. NavMeshAgent:

    • Using the NavMeshAgent component to control AI movement.
    • Configuring NavMeshAgent properties.
  4. Basic AI Behaviors:

    • Implementing patrolling behavior.
    • Implementing chasing behavior.

AI Basics

Understanding AI in Games

AI in games refers to the simulation of intelligent behavior in non-player characters (NPCs). Common AI behaviors include:

  • Patrolling: NPCs move along a predefined path.
  • Chasing: NPCs follow the player or another target.
  • Fleeing: NPCs move away from a threat.

Common AI Behaviors

  • Patrolling: NPCs move along a predefined path.
  • Chasing: NPCs follow the player or another target.
  • Fleeing: NPCs move away from a threat.

NavMesh

Introduction to Unity's Navigation Mesh (NavMesh)

A NavMesh is a data structure that defines the walkable surfaces in your game environment. It allows AI characters to navigate the scene intelligently.

Setting Up a NavMesh

  1. Bake the NavMesh:

    • Go to Window > AI > Navigation.
    • In the Navigation window, select the Bake tab.
    • Adjust the settings as needed and click Bake.
  2. Add NavMeshAgent to NPC:

    • Select your NPC GameObject.
    • Add a NavMeshAgent component (Component > Navigation > NavMeshAgent).

NavMeshAgent

Using the NavMeshAgent Component

The NavMeshAgent component allows you to control the movement of AI characters along the NavMesh.

Configuring NavMeshAgent Properties

  • Speed: Controls the movement speed of the agent.
  • Angular Speed: Controls how quickly the agent can change direction.
  • Stopping Distance: The distance from the target at which the agent will stop.

Basic AI Behaviors

Implementing Patrolling Behavior

  1. Create Waypoints:

    • Create empty GameObjects to serve as waypoints.
    • Position them in the scene where you want the NPC to patrol.
  2. Script for Patrolling:

using UnityEngine;
using UnityEngine.AI;

public class PatrollingAI : MonoBehaviour
{
    public Transform[] waypoints;
    private int currentWaypointIndex = 0;
    private NavMeshAgent agent;

    void Start()
    {
        agent = GetComponent<NavMeshAgent>();
        agent.SetDestination(waypoints[currentWaypointIndex].position);
    }

    void Update()
    {
        if (agent.remainingDistance < agent.stoppingDistance)
        {
            currentWaypointIndex = (currentWaypointIndex + 1) % waypoints.Length;
            agent.SetDestination(waypoints[currentWaypointIndex].position);
        }
    }
}

Implementing Chasing Behavior

  1. Script for Chasing:
using UnityEngine;
using UnityEngine.AI;

public class ChasingAI : MonoBehaviour
{
    public Transform target;
    private NavMeshAgent agent;

    void Start()
    {
        agent = GetComponent<NavMeshAgent>();
    }

    void Update()
    {
        agent.SetDestination(target.position);
    }
}

Practical Exercise

Exercise: Create a Patrolling and Chasing NPC

  1. Setup:

    • Create a new scene.
    • Add a plane to serve as the ground.
    • Add a few obstacles to the scene.
    • Create an NPC GameObject and add a NavMeshAgent component.
  2. Patrolling:

    • Create waypoints and attach the PatrollingAI script to the NPC.
    • Test the patrolling behavior.
  3. Chasing:

    • Create a player GameObject.
    • Attach the ChasingAI script to the NPC and assign the player as the target.
    • Test the chasing behavior.

Solution

  1. Setup:

    • Create a new scene and add a plane.
    • Add obstacles and an NPC with a NavMeshAgent.
  2. Patrolling:

    • Create waypoints and attach the PatrollingAI script.
    • Test the NPC patrolling between waypoints.
  3. Chasing:

    • Create a player GameObject.
    • Attach the ChasingAI script to the NPC and assign the player as the target.
    • Test the NPC chasing the player.

Common Mistakes and Tips

  • NavMesh Not Baked: Ensure the NavMesh is baked correctly.
  • Waypoints Not Assigned: Make sure waypoints are assigned in the inspector.
  • Target Not Assigned: Ensure the target is assigned in the inspector for chasing behavior.

Conclusion

In this section, we covered the basics of AI scripting in Unity. We learned how to set up a NavMesh, use the NavMeshAgent component, and implement basic AI behaviors like patrolling and chasing. These foundational skills will enable you to create more complex AI behaviors in your games. In the next section, we will delve into advanced physics techniques to further enhance your game development skills.

© Copyright 2024. All rights reserved