Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Didymos · Jan 03, 2011 at 06:55 PM · javascriptaidisablewaypoint

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;

}

Comment
Add comment · Show 4
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image PrimeDerektive · Jan 03, 2011 at 07:01 PM 0
Share

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

avatar image Didymos · Jan 03, 2011 at 07:01 PM 0
Share

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

avatar image PrimeDerektive · Jan 03, 2011 at 07:05 PM 0
Share

Disable the GameObject, or just this script?

avatar image Didymos · Jan 03, 2011 at 07:09 PM 0
Share

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

2 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

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;
Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image NinjaSquirrel · Jan 03, 2011 at 07:15 PM 0
Share

That would be a better way.

avatar image Didymos · Jan 03, 2011 at 07:34 PM 0
Share

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

avatar image Didymos · Jan 03, 2011 at 08:34 PM 0
Share

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;



}

avatar image Didymos · Jan 03, 2011 at 08:43 PM 0
Share

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?

avatar image Didymos · Jan 03, 2011 at 09:33 PM 0
Share

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.

avatar image
1

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Didymos · Jan 03, 2011 at 07:11 PM 0
Share

Thanks, I will try that.

avatar image NinjaSquirrel · Jan 03, 2011 at 07:12 PM 0
Share

No problem. :)

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

1 Person is following this question.

avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges