Enemy AI(patrolling, field of view and listening)

Hi everyone I need help making an enemy ai (patrolling listening and field of view) i was searching in google and here in unity answers but couldnt anything helpfull.
Any help like videos websites or scripts for making an enemy ai would be great.
I really cant make an enemy patrol script :frowning: please help me.
Thanks in advance

You can take a look at a ready.made (mostly) pathfinding soluton like the Astar Pathfinding by Aron Granberg. Be prepared to spend many hours, days and even weeks on it because pathfinding isn’t exactly one of the easiest things to do in game, especially if you are inexperienced. that being said, don’t be discouraged and if you hit any obstacles, maybe just work on some other bits of your game first to learn.

Lucky for you, I recently made a simple AI script, here you go.

  1. Make two new Javascripts,

  2. Name one “enemyAI”

  3. name the other, “enemyhealth”

  4. copy all of this and put it into the enemyAI script,

    var waypoints : Transform;
    var Target : Transform;
    var roam : enemyhealth;
    var mybody : GameObject;

    function Start() {

    roam = mybody.GetComponent(enemyhealth);

    InvokeRepeating(“check”,0,10);
    InvokeRepeating(“MOVEIT”,0,0.01);

    }

    function check() {

    if(roam.isroaming==true){
    var index : int = Random.Range(1,waypoints.length);
    var thePrefab : Transform = waypoints[index];

    Target = thePrefab;

    }
    }

    function MOVEIT() {
    if(roam.isroaming==true){
    var rotation = Quaternion.LookRotation(Target.position - transform.position);
    transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime);

    transform.Translate(Vector3.forward * Time.deltaTime);
    }
    }

And this into the enemyhealth,

var HP : int = 100;
var body : GameObject;

var Distance;
var Target : Transform;
var lookAtDistance = 25.0;
var attackRange = 15.0;
var moveSpeed = 5.0;
var Damping = 6.0;

var isroaming : boolean = true;




 
function Update ()
{
    Distance = Vector3.Distance(Target.position, transform.position);
 
    if (Distance < lookAtDistance)
    {
       
       lookAt();
       isroaming = false;
    }
 
    if (Distance > lookAtDistance)
    {
       
       isroaming = true;
    }
 
    if (Distance < attackRange)
    {
       attack ();
    }
    if(HP < 1){
    Instantiate(body, transform.position, transform.rotation);
    DestroyObject (gameObject);
    }
    

}
 
function lookAt ()
{
    var rotation = Quaternion.LookRotation(Target.position - transform.position);
    transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime*35);
}
 
function attack ()
{
    transform.Translate(Vector3.forward * Time.deltaTime * 3);
}

What you do next, is put empty gameobjects to the map, put them where you want your enemy to be, and tag them with “waypoint” by going to project settings, go into the tags section, then open up the closed array at the top of the list. make a new one and call it “waypoint”. Put both these javascripts into a cube, and make sure it has a character controller on it, or else it wont work. when in the inspector, find the section that is the array of waypoints, and drag your waypoints into the variables array. Be aware, the first on the list will be ignored, so put it there twice.

in the enemyhealth script, put your character in the target slot, and for the body variable, put a corpse prefab you want.

To do this, make a cube, put a rigidbody in it, and place this script on it,

var timeOut = 1.0;
var detachChildren = false;

function Awake ()
{
	Invoke ("DestroyNow", timeOut);
}

function DestroyNow ()
{
	if (detachChildren) {
		transform.DetachChildren ();
	}
	DestroyObject (gameObject);
}

save this cube as a prefab, and assign it to your enemys body variable.

and you’re done :3

i have a simple path finding script that requires pro version. which you can try the trial. It does not have line of sight or a view area. its just behaves based on how close you are to it. But you could achieve this by putting an invisible collider infront of it and have it detect a collision or with ray casts.

For this to work you need to set up a nav mesh and your enemy needs to have a navmeshagent.

var target : Transform;
var Distance;
var lookAtDistance : float = 14.0;
var chaseDistance : float = 7.0;
var closeStopDistance : float = 5.0;
var rotationSpeed : float = 3.0;


var agent: NavMeshAgent; 

function Start(){

}


function Update()
{

	Distance = Vector3.Distance(target.position, transform.position);
	
	if (Distance > lookAtDistance)
	{
		
		renderer.material.color = Color.green;
	}
	if (Distance < chaseDistance){
			agent.SetDestination(target.position);
			var mycolor : Color = new Color(1.0F, 0.6F, 0F);
			renderer.material.color = mycolor;
	}
	if (Distance < lookAtDistance){
		var rotation = Quaternion.LookRotation(target.position - transform.position);
		transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * rotationSpeed);
		if (Distance > chaseDistance){
			renderer.material.color = Color.yellow;
		}
	}
	if (Distance < closeStopDistance){
		agent.Stop(false);
		renderer.material.color = Color.red;
            //PUT YOUR ATTACK CODE HERE.
	}
		
			
					

}