Unity NPC and Enemy AI Scripts
This document explains how to create NPCs and enemies in Unity using C#. It includes basic
patrolling, player detection, chasing, and attacking behaviors, with easy-to-follow examples.
1. NPC with Basic Movement
An NPC can move between waypoints to simulate patrolling. The following script
demonstrates how to achieve this behavior.
Unity C# Script :
using UnityEngine;
public class NPCPatrol : MonoBehaviour
{
public Transform[] waypoints; // Array of waypoints
public float speed = 2f; // Movement speed
private int currentWaypointIndex = 0;
void Update()
{
if (waypoints.Length == 0) return;
// Move towards the current waypoint
Transform targetWaypoint = waypoints[currentWaypointIndex];
transform.position = Vector3.MoveTowards(transform.position,
targetWaypoint.position, speed * Time.deltaTime);
// Check if the NPC reached the waypoint
if (Vector3.Distance(transform.position, targetWaypoint.position) < 0.1f)
{
// Move to the next waypoint, loop back if at the end
currentWaypointIndex = (currentWaypointIndex + 1) % waypoints.Length;
}
}
}
2. Enemy with Player Detection and Chasing
This script allows the enemy to detect the player within a certain range and chase them.
C# Script:
using UnityEngine;
public class EnemyAI : MonoBehaviour
{
public Transform player; // Reference to the player
public float detectionRange = 5f; // Distance to detect the player
public float speed = 3f; // Movement speed
void Update()
{
// Check the distance to the player
float distanceToPlayer = Vector3.Distance(transform.position, player.position);
if (distanceToPlayer < detectionRange)
{
// Chase the player
transform.position = Vector3.MoveTowards(transform.position, player.position,
speed * Time.deltaTime);
}
}
}
3. Enemy with Attack Behavior
The following script adds attack behavior to the enemy when they are within an attack
range.
C# Script:
using UnityEngine;
public class EnemyAttack : MonoBehaviour
{
public Transform player; // Reference to the player
public float detectionRange = 5f; // Distance to detect the player
public float attackRange = 1f; // Distance to attack the player
public float speed = 3f; // Movement speed
public int damage = 10; // Damage amount
void Update()
{
// Check the distance to the player
float distanceToPlayer = Vector3.Distance(transform.position, player.position);
if (distanceToPlayer < detectionRange)
{
if (distanceToPlayer > attackRange)
{
// Chase the player
transform.position = Vector3.MoveTowards(transform.position, player.position,
speed * Time.deltaTime);
}
else
{
// Attack the player
Debug.Log("Enemy attacks the player and deals " + damage + " damage!");
// Add your damage logic here
}
}
}
}
4. NPC Interaction
NPCs can be interactive, providing messages or other actions when the player is near. This
script demonstrates how to create a trigger-based interaction system.
C# Script:
using UnityEngine;
public class NPCInteraction : MonoBehaviour
{
public string npcMessage = "Hello, traveler!"; // Message to display
void OnTriggerEnter(Collider other)
{
if (other.CompareTag("Player"))
{
Debug.Log(npcMessage); // Display the NPC's message
}
}
}