Simple AI pathing and patrolling set of scripts

I have been working on set of AI scripts to make a walking and patrolling enemy. There are 2 scripts, the first listed is enemyAI, which is a slightly adapted form of script I found while trying to make, this, and the other is called enemyPATHING.

here’s the enemyAI

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


var isroaming : boolean = true;



function Update ()
{
   player = GameObject.FindWithTag("Player");
   Target = player.transform;
   Distance = Vector3.Distance(Target.position, transform.position);

   if (Distance < lookAtDistance)
   {

      lookAt();
      isroaming = false;
   }

   if (Distance > lookAtDistance)
   {

      isroaming = true;
   }

   if (Distance < attackRange)
   {
      attack ();
   }
  
  
  
  


}

function lookAt ()
{
   transform.LookAt(Target.transform);
}

function attack ()
{
   rigidbody.AddRelativeForce(Vector3.forward * moveSpeed, ForceMode.Force);

}

and this is the enemyPATHING

#pragma strict

var thewaypoint : GameObject;
var Target : Transform;
var roam : enemyAI;
var mybody : GameObject;
var speed : int = 4;
var refreshrate : int = 2;

function Start() {





roam = mybody.GetComponent(enemyAI);


InvokeRepeating("check",0,refreshrate);
InvokeRepeating("MOVEIT",0,0.01);
}



function Update (){

Target = thewaypoint.transform;

}


function check() {

if(roam.isroaming==true){



var index : int = Random.Range(1,thewaypoint.GetComponent(Waypoint).waypoints.length);
var thePrefab : GameObject = thewaypoint.GetComponent(Waypoint).waypoints[index];

thewaypoint = thePrefab;

}
}
function MOVEIT() {
if(roam.isroaming==true){
transform.LookAt(Target.transform);
rigidbody.AddRelativeForce(Vector3.forward * speed, ForceMode.Force);
}
}

A example scene of how these work is displayed with 6 waypoints, and a blank test enemy cube. the first waypoint the AI will follow is represented in red.

Imgur

copy all the values in shown in the enemies scripts or adapt them to your use.

in enemyPATHING:

‘thewaypoint’ and ‘Target’ should be the same object, and should have the waypoint script, which will be listed later. In the ‘roam’ section, drag your enemy from the inspector into this value, so it should be itself. ‘mybody’ should also be the enemy itself. ‘speed’ is the rate of which it moves, make sure this is proportional to the mass of the enemies rigidbody, or it will move very slowly or way too fast. ‘refreshrate’ is how often the enemy should choose a new target. make this bigger if the waypoints are spread further apart, and make it small if the waypoints are bunched up and close together.

in enemyAI:

‘Target’ should be the player character. ‘Look at distance’ is how far the enemy can see the player. ‘attack range’ is how close the player can be until it rushes towards the player, at the speed of ‘move speed’. ‘is roaming’ is a boolean to determine if the enemy has noticed the enemy.

How the enemy player detection works, is that the enemy pathfinding will only work if the ‘is roaming’ is true. when the character gets within ‘look at distance’, it turns off. make the look at distance higher than attack range if you want a long range enemy, and make them the same if they are a melee enemy that charges you.

How to setup waypoints is easy. create an empty gameobject, place any kind of light in it, and turn its intensity and range to 0. this is so you can see where the waypoints are in the editor. and place this one lined script in it,

var waypoints : GameObject[];

and place them around where your level is. how this works s that the enemy will choose randomly a waypoint within the ‘waypoints’ value, and go there, and then choose one of it’s assigned waypoints. configure the chosen points as desired if you want to make it a one-way path, or an intersection, or any other pathing.here is a picture showing an example.

Imgur

if you have any questions or comments, or find a bug, please let me know.

I was thinking about using this with the Fpskit_v1.3.5.I have infinite ground script i found on youtube.That i am using with it.This is a great resource thank you.