How to Make a Horror Enemy AI in Unity || Part 3 - Sounds & Search Function

preview_player
Показать описание
This is the last part 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.

#Unity #Unity3D #gamedev

*Scripts 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! 👍

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:
Рекомендации по теме
Комментарии
Автор

The scripts:

Enemy AI 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, detectionDistance, catchDistance, searchDistance, minChaseTime, maxChaseTime, minSearchTime, maxSearchTime, jumpscareTime;
public bool walking, chasing, searching;
public Transform player;
Transform currentDest;
Vector3 dest;
public Vector3 rayCastOffset;
public string deathScene;
public float aiDistance;
public GameObject hideText, stopHideText;

void Start()
{
walking = true;
currentDest = destinations[Random.Range(0, destinations.Count)];
}
void Update()
{
Vector3 direction = (player.position -
RaycastHit hit;
aiDistance = Vector3.Distance(player.position, this.transform.position);
+ rayCastOffset, direction, out hit, detectionDistance))
{
== "Player")
{
walking = false;
StopCoroutine("stayIdle");


searching = true;
}
}
if(searching == true)
{
ai.speed = 0;
aiAnim.ResetTrigger("walk");
aiAnim.ResetTrigger("idle");

aiAnim.SetTrigger("search");
if(aiDistance <= searchDistance)
{
StopCoroutine("stayIdle");



chasing = true;
searching = false;
}
}
if(chasing == true)
{
dest = player.position;
ai.destination = dest;
ai.speed = chaseSpeed;
aiAnim.ResetTrigger("walk");
aiAnim.ResetTrigger("idle");

aiAnim.SetTrigger("sprint");
if (aiDistance <= catchDistance)
{

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

hideText.SetActive(false);




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;
}
}
}
public void stopChase()
{
walking = true;
chasing = false;

currentDest = destinations[Random.Range(0, destinations.Count)];
}
IEnumerator stayIdle()
{
idleTime = Random.Range(minIdleTime, maxIdleTime);
yield return new WaitForSeconds(idleTime);
walking = true;
currentDest = destinations[Random.Range(0, destinations.Count)];
}
IEnumerator searchRoutine()
{
yield return new WaitForSeconds(Random.Range(minSearchTime, maxSearchTime));
searching = false;
walking = true;
currentDest = destinations[Random.Range(0, destinations.Count)];
}
IEnumerator chaseRoutine()
{
yield return new WaitForSeconds(Random.Range(minChaseTime, maxChaseTime));
stopChase();
}
IEnumerator deathRoutine()
{
yield return new

}
}

Omogonix
Автор

Cool bro nice works i hope you get popular fast

seesooner
Автор

Thank you so much for these tutorials! They're really helping me out with my game :D

frankstercodestuff
Автор

Thank you for this tutorial series. It's so simple to understand the code flow and how you are applying it.
IMO other tutorials should work in a more simplistic way, for better understanding other than going straightfoward to the "right way" of coding.
If it works, that's the right way. It's simple, do it first. Later, make it better.

vitobrx
Автор

OMG, thank you very much! I was struggling in the last few days with scripting audio and now you just solved my problem eazy peazy.

Nemko
Автор

Thx for the script! You really helped me!(sorry for my bad english, im not from UK or US)

Beksu_
Автор

bro si life saver still working but without screaching :)

kaszana
Автор

Thank you so much for this can you make him able to open doors ? Plz upload tutorial about this

hassango
Автор

Thank you so much! will you do a death scene? :D and I still have a problem with a Camera inside Monster, it. still say "No cameras rendering"

BigbearDev
Автор

thanks for the tutorial how would i make a sound for when the ai spots you?

RyanKitchen-xc
Автор

When something falls from your hand, you can make the enemy search for you by its sound. !!! Like In The Granny Games???

gaurav_goyani
Автор

can the ai loose you when you are far from it?

DEV_-ghgl
Автор

Hello! Why when monster turning on Chasing, my game freezing on 1-0.3 second, how to fix it?

klaydxD
Автор

Is it possible to when the Monster saws you going to a hiding spot. it kills you?

kingiwakes_official
Автор

How to make jumpscare after caught by snow man please tell

prateekvishwakarma
Автор

How it can walk on stair pls tell thanks you so much 😁

buntygamer
Автор

Can you pls make it stop chaseing after time he don t sees you?

DarkGamesPlays
Автор

Is the AI uses raycast ? Like if player is behind the wall.. will the enemy see player?

LoopX