x


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;

}

more ▼

asked Jan 03 '11 at 06:55 PM

Didymos gravatar image

Didymos
25 10 10 17

Do you only have one waypoint, or an array of waypoints?

Jan 03 '11 at 07:01 PM PrimeDerektive

I have just one waypoint currently. I am simply just trying to disable the GameObject once it reaches the waypoint.

Jan 03 '11 at 07:01 PM Didymos

Disable the GameObject, or just this script?

Jan 03 '11 at 07:05 PM PrimeDerektive

Well I simply want to disable the GameObject once it is done with the waypoint.

Jan 03 '11 at 07:09 PM Didymos
(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

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;
more ▼

answered Jan 03 '11 at 07:12 PM

PrimeDerektive gravatar image

PrimeDerektive
3.1k 57 64 84

That would be a better way.

Jan 03 '11 at 07:15 PM NinjaSquirrel

Okay I will try all of your ideas. Thanks for the prompt replies, I appreciate it immensely.

Jan 03 '11 at 07:34 PM Didymos

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;

}

Jan 03 '11 at 08:34 PM Didymos

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?

Jan 03 '11 at 08:43 PM Didymos

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.

Jan 03 '11 at 09:33 PM Didymos
(comments are locked)
10|3000 characters needed characters left

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.

more ▼

answered Jan 03 '11 at 07:09 PM

NinjaSquirrel gravatar image

NinjaSquirrel
759 31 41 69

Thanks, I will try that.

Jan 03 '11 at 07:11 PM Didymos

No problem. :)

Jan 03 '11 at 07:12 PM NinjaSquirrel
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x3453
x3325
x957
x215
x170

asked: Jan 03 '11 at 06:55 PM

Seen: 1445 times

Last Updated: Jan 03 '11 at 08:43 PM