How to Make a Horror Enemy AI in Unity || Part 1 - The General AI

preview_player
Показать описание
This is part 1 of a short 3 part Unity tutorial series where I teach you guys how to make a horror enemy AI in Unity using C# that patrols around and chases the player when they've spotted them.

In the next part, I'll be making a hiding system.

#Unity #Unity3D #gamedev

*Script will be in pinned comment.*

For more Unity tutorials like this or more videos in general - be sure to like, comment, and subscribe for more! 👍

Part 2:

Play Beware The Night Guard:

Wishlist Bodhi 'n' Friends on Steam now:

Follow me on Twitter:

Try out my games:

My Website:

Subscribe to my Second Channel:

Join my Discord:

Facebook Page:
Рекомендации по теме
Комментарии
Автор

To anybody experiencing any bugs, make sure you watch part 2 and 3 where I iron out some stuff with the enemy AI.
EDIT: I've updated the part 1 script so any minor issues are now ironed out with it recently. No need to watch part 2 and 3 unless you want extra features for your AI now!

Script:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
using UnityEngine.SceneManagement;

public class EnemyAI : MonoBehaviour
{
public NavMeshAgent ai;
public List<Transform> destinations;
public Animator aiAnim;
public float walkSpeed, chaseSpeed, minIdleTime, maxIdleTime, idleTime, sightDistance, catchDistance, chaseTime, minChaseTime, maxChaseTime, jumpscareTime;
public bool walking, chasing;
public Transform player;
Transform currentDest;
Vector3 dest;
int randNum;
public int destinationAmount;
public Vector3 rayCastOffset;
public string deathScene;

void Start()
{
walking = true;
randNum = Random.Range(0, destinations.Count);
currentDest = destinations[randNum];
}
void Update()
{
Vector3 direction = (player.position -
RaycastHit hit;
if + rayCastOffset, direction, out hit, sightDistance))
{
if (hit.collider.gameObject.tag == "Player")
{
walking = false;
StopCoroutine("stayIdle");


chasing = true;
}
}
if (chasing == true)
{
dest = player.position;
ai.destination = dest;
ai.speed = chaseSpeed;
aiAnim.ResetTrigger("walk");
aiAnim.ResetTrigger("idle");
aiAnim.SetTrigger("sprint");
float distance = Vector3.Distance(player.position, ai.transform.position);
if (distance <= catchDistance)
{

aiAnim.ResetTrigger("walk");
aiAnim.ResetTrigger("idle");



chasing = false;
}
}
if (walking == true)
{
dest = currentDest.position;
ai.destination = dest;
ai.speed = walkSpeed;

aiAnim.ResetTrigger("idle");
aiAnim.SetTrigger("walk");
if (ai.remainingDistance <= ai.stoppingDistance)
{

aiAnim.ResetTrigger("walk");
aiAnim.SetTrigger("idle");
ai.speed = 0;
StopCoroutine("stayIdle");
StartCoroutine("stayIdle");
walking = false;
}
}
}
IEnumerator stayIdle()
{
idleTime = Random.Range(minIdleTime, maxIdleTime);
yield return new WaitForSeconds(idleTime);
walking = true;
randNum = Random.Range(0, destinations.Count);
currentDest = destinations[randNum];
}
IEnumerator chaseRoutine()
{
chaseTime = Random.Range(minChaseTime, maxChaseTime);
yield return new WaitForSeconds(chaseTime);
walking = true;
chasing = false;
randNum = Random.Range(0, destinations.Count);
currentDest = destinations[randNum];
}
IEnumerator deathRoutine()
{
yield return new

}
}

Omogonix
Автор

Super tutorial !! Thank you so much for the hiding system coming to next tutorial. Its so cool to have practical examples and gamelogic to learn for us non coders. It helps a lot ! Cheers from Belgium!

dhidavidhuys
Автор

this is the best tutorials and a lifesaver .thanks so much❤😭

scorpiowisp
Автор

I was having loads of problems with the ray cast hitting the model of the ai so I turned off the colliders for it and it worked fine after thanks.

TheBogeyman
Автор

Is there a way to make it so the ai cannot start chasing you when you are behind him?

chumeomuadong
Автор

Helped Me Develop One Of My Dream Horror Games :)

itsdonix
Автор

I've been running into a bug, not sure if you've already fixed it, but I was using the script in the pinned comment. Basically, if I approach the enemy from behind, it instantly catches me from a long distance without even turning around. Not sure what's causing it.

toaofanimation
Автор

Hey bro, You can make a examine System and hidding places system?
Good tutorial, i wanted the part 2😉

Alex_xx-lgcj
Автор

The way I use this is to use enums for the states and I use switch statements, and for the ai learning part I just use an array of bools that detects the actions of the player (like hiding in a certain spot too much) that will turn on after so many times and will make the Npc check that spot more often and will become more aware.

GunGunAnimated
Автор

I don't know why but the only anim it works is my idle anim. i ve checked everything any transition or setting in the animator and in the inspector, please help. I mean it starts with the idle anim and it walks with the idle anim.

georgechristopoulos
Автор

help the parameter doesnt allow me to add any parmeters how do i fix this

Realdestroyer
Автор

hey, can you make a tutorial on how to make ai hear a sound, like when you open a door or make a sound that will make ai take the place where the sound is coming from as its destination?

imisstheoldmee
Автор

For some reason the jumpscare is giving me issues something about a display 1??

DriplordVR
Автор

can anyone help me with this error Scene 'New Scene' couldn't be loaded because it has not been added to the build settings or the AssetBundle has not been loaded.
To add a scene to the build settings use the menu File->Build Settings...

someguy
Автор

This is granny like AI
Cool dude now i can make that type of ai 😂

Not_Chen
Автор

I copy getting error code cs1022 how do you fix that

Oscarboboscar
Автор

i got problem with cameras on jumpscares, if ill switch off zombie will run around all day, byt if ill switch on it will play only animation, so in animator jumpscare is not working, HELP PLS

Nikitoskiy
Автор

Man i really wish you had a voice over while doing this. I appreciate the videos tho

bradfab_fx
Автор

This is great, but for some reason my dude ain't facing the direction he's walking. Any ideas why?

OldManOrds
Автор

im having a big problem, the sprint and jumpscare dont play, i've been struggling for an hour, i tried everything (changing raycast offset etc...)

Kleiner_Gmod