Basic Patrolling Enemy AI in Unity - Unity C# Tutorial

Опубликовано: 03 Октябрь 2024
на канале: Omogonix
1,565
31

In this Unity tutorial, I show you guys how to make a basic patrolling enemy AI in Unity. This is not an advanced tutorial, just something more basic and simple.

#Unity #Unity3D #Unity2021

For more Halo Infinite videos or Halo videos in general - be sure to like, comment, subscribe, and turn on notifications for more! 👍

Script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;

public class chasePlayer : MonoBehaviour
{
public Vector3 destination;
public Transform Player, patrol;
public NavMeshAgent agent;
public GameObject cube;
public bool spotted;
public float searchTime;

void Update(){
if(spotted == false){
cube.SetActive(false);
destination = patrol.position;
agent.destination = destination;
}
if(spotted == true){
cube.SetActive(true);
destination = Player.position;
agent.destination = destination;
}
}
void OnTriggerEnter(Collider other){
if(other.CompareTag("Player")){
spotted = true;
}
}
void OnTriggerExit(Collider other){
if(other.CompareTag("Player")){
StartCoroutine(search());
}
}
IEnumerator search(){
yield return new WaitForSeconds(searchTime);
spotted = false;
}
}

Follow me on Twitter:
  / omogonix  

Try out my games:
https://omogonixlachlan.itch.io

Forgehub:
https://www.forgehub.com/members/omog...

Subscribe to my Second Channel:
   / @lachlanshelton129  

Join my Discord:
  / discord  

Facebook Page:
  / omogonix  

Follow me on Instagram:
  / uwugonixhalo  

Music Used:
glue70 - Black Rock
Lost Sky - Dreams [NCS Release]
C418 - Blocks