0% found this document useful (0 votes)
2 views

Unity NPC and Enemy Scripts

Uploaded by

yakshgoyal0
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Unity NPC and Enemy Scripts

Uploaded by

yakshgoyal0
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

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
}
}
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy