- Home /
How do I create a simply disable script for enemy AI once it completes its waypoints?
I have looked around at least three days now and I can't seem to find anything about incorporating a disable script along side a waypoint script.
Essentially I want the AI to become disabled as soon as it has finished its last waypoint. How would I begin to do this? Below is the script.
SCRIPT
EDIT-> I have changed the original script to support the ones the people who have answered my question. Thanks!
var waypoint : Transform;
var speed : float = 20;
var distanceToTarget : float = Vector3.Distance(transform.position, EvilCube);
function Update() {
var target : Vector3 = waypoint.position;
var moveDirection : Vector3 = target - transform.position;
var velocity = rigidbody.velocity;
if(moveDirection.magnitude < 1){
velocity = Vector3.zero;
if(distanceToTarget == 0)
this.EvilCube.active = false;
}
else{
velocity = moveDirection.normalized * speed;
}
rigidbody.velocity = velocity;
}
Do you only have one waypoint, or an array of waypoints?
I have just one waypoint currently. I am simply just trying to disable the GameObject once it reaches the waypoint.
Well I simply want to disable the GameObject once it is done with the waypoint.
Answer by PrimeDerektive · Jan 03, 2011 at 07:12 PM
calculate the distance between this object and the waypoint position (in your script, "target"), using Vector3.Distance:
var distanceToTarget : float = Vector3.Distance(transform.position, target);
Then check if the distance is zero, which would mean you reached the target, and disable the gameobject:
if(distanceToTarget == 0)
this.gameObject.active = false;
Okay I will try all of your ideas. Thanks for the prompt replies, I appreciate it immensely.
Is this the right way then? Script listed below
var waypoint : Transform;
var speed : float = 20;
var distanceToTarget : float = Vector3.Distance(transform.position, EvilCube);
function Update() {
var target : Vector3 = waypoint.position;
var moveDirection : Vector3 = target - transform.position;
var velocity = rigidbody.velocity;
if(moveDirection.magnitude < 1){
velocity = Vector3.zero;
if(distanceToTarget == 0)
this.EvilCube.active = false;
}
else{
velocity = moveDirection.normalized * speed;
}
rigidbody.velocity = velocity;
}
I edited my question and changed the original script I had posted I posted a new script with the suggestion that you have advised me. Did I do it correctly?
Your script works all except the first part "var distanceToTarget : float = Vector3.Distance(transform.position, target);" I found that if I simply put ""var distanceToTarget : float" that the object is disabled. +1 and best answer.
Answer by NinjaSquirrel · Jan 03, 2011 at 07:09 PM
I would put a variable in there and name it "waypointReached", make it a boolean. start of with it's boolean false, then once it has reaches the waypoint change it to true. then add code, where if waypointReached is true, destroy the object. Hope that helps some.
Your answer
Follow this Question
Related Questions
What should I do? Disable the AI or have the AI simply be recylced and reused? 1 Answer
How to make enemy attack multiple unit rather then single?(updated) 1 Answer
How Can I Change My Player Speed If A Variable Is Set To Zero 1 Answer
How to create an AI follow script in Java? 2 Answers
AI Path and respawn help!!! 0 Answers