- Home /
Monster Patrolling
Greetings, I'm making a game where there's a monster should be patrolling and chase me when he see me, and leave me if I run away. So far I made the monster chase me when I'm close and stop when i run away Just need to make it patrol, I KNOW how to make way-points but don't know the script to make the monster walk on them. Here's the script I'm using for the monster to chase me(JS) :
var target : Transform; //the enemy's target
var moveSpeed = 3; //move speed
var rotationSpeed = 3; //speed of turning
var range : float=10f;
var range2 : float=10f;
var stop : float=0;
var myTransform : Transform; //current transform data of this enemy
function Awake()
{
myTransform = transform; //cache transform data for easy access/preformance
}
function Start()
{
target = GameObject.FindWithTag("Player").transform; //target the player
}
function Update () {
//rotate to look at the player
var distance = Vector3.Distance(myTransform.position, target.position);
if (distance<=range2 && distance>=range){
myTransform.rotation = Quaternion.Slerp(myTransform.rotation,
Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime);
}
else if(distance<=range && distance>stop){
//move towards the player
myTransform.rotation = Quaternion.Slerp(myTransform.rotation,
Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime);
myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
}
else if (distance<=stop) {
myTransform.rotation = Quaternion.Slerp(myTransform.rotation,
Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime);
}
}
Answer by ShadoX · Jul 31, 2014 at 11:40 AM
Assuming that you know how to take care of obstacles and those things, then you could just keep track of the wayPoints in an array / list
List wayPoints = new List();
//just add the empty gameObjects that work as wayPoints ot the list
and have the Monter walk towards 1 point and once it's at the point or close enough, you could switch to the next wayPoint.
A easy way would be to use NavMeshAgent (check https://www.youtube.com/watch?v=mP7ulMu5UkU ) and use SetDestination.