- Home /
Question by
WesterlyCarrot9 · Feb 21, 2014 at 06:07 PM ·
javascriptvector3aiwaypointpoint
Kind of Patrol AI with Point A and B Help
Ok so i use the script below to have a game object move itself from Point A to Point B and possibly then back and start all over again just like a patrol AI script. The thing is that something in my function Patrol(){
prevents it from moving back to Point A. It looks at it back doesn't move to it. What am i doing wrong? Also, is there a simpler way than this to make that function from Point A to Point B and then back? Thanks.
var player : Transform;
var PointA : Transform;
var PointB : Transform;
var controller : CharacterController;
var speed : float;
var range : float;
var roar : AudioClip;
var soundplayed : boolean;
function Start(){
soundplayed = false;
}
function Update(){
Debug.Log(Vector3.Distance(transform.position, PointB.position));
if(InRange()){
if(!soundplayed){
audio.PlayOneShot(roar);
soundplayed = true;
}
GoToPlayer();
}
else{
Patrol();
}
}
function InRange(){
if(Vector3.Distance(transform.position, player.position) < range){
return true;
}
else{
return false;
}
}
function GoToPlayer(){
transform.LookAt(player.position);
controller.SimpleMove(transform.forward * speed);
}
function Patrol(){
if(Vector3.Distance(transform.position, PointB.position) > 2){
transform.LookAt(PointB.position);
controller.SimpleMove(transform.forward * speed);
if(Vector3.Distance(transform.position, PointB.position) < 2){
transform.LookAt(PointA.position);
controller.SimpleMove(transform.forward * speed);
}
}
}
Comment
Your answer
Follow this Question
Related Questions
Help with a simple AI 2 Answers
Waypoint System help 1 Answer
how to create a waypoint? 2 Answers
How do I create a simply disable script for enemy AI once it completes its waypoints? 2 Answers