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 31, 2011 at 11:25 PM · javascriptairespawn

How to add respawn to enemy AI?

I am creating a scenario where you have to fight off a certain enemy before he/she overwhelms you. If you don't do it fast enough the enemy takes over. Here is the script. I have tried many times, but the enemy won't respawn. I can make the "Player" respawn, however I can't seem to do it with the AI. Here is the script. I use Javascript. I placed the script on the enemy GameObject.

var spawnRange = 0.0; // the distance within which the enemy should be active. var gizmoName : String; // the type of the object. (See OnDrawGizmos() for more.) var enemyPrefab : GameObject; // link to the Prefab we'll be instantiating / destroying on demand.

// Cache variables, used to speed up the code. private var player : Transform; private var currentEnemy : GameObject; private var wasOutside = true;

// Called on Scene startup. Cache a link to the Player object. // (Uses the tagging system to locate him.) function Start () { player = GameObject.FindWithTag("Player").transform; }

// Called at least once every game cycle. This is where the fun stuff happens. function Update () { // how far away is the player? var distanceToPlayer = Vector3.Distance(transform.position, player.position);

// is he in range? if (distanceToPlayer < spawnRange) { // in range. Do we have an active enemy and the player has just come into range, instantiate the prefab at our location. if (!currentEnemy && wasOutside) currentEnemy = Instantiate(enemyPrefab, transform.position, transform.rotation);

 // player is now inside our range, so set the flag to prevent repeatedly instantiating the prefab.
 wasOutside = false;

} // player is out of range. else {
// is player leaving the sphere of influence while our prefab is active? if (currentEnemy && !wasOutside) Destroy(currentEnemy); // kill the prefab...

 // ...and set our flag so we re-instantiate the prefab if the player returns.
 wasOutside = true;

}

}

// Called by the Unity Editor GUI every update cycle. // Draws an icon at our transform's location. The icon's filename is derived from the "type" variable, which allows this script to be used for any enemy. function OnDrawGizmos () { Gizmos.color = Color(1, 1, 1, 1);

// See the help docs for info on where the icon needs to be stored for this function to work.
Gizmos.DrawIcon(transform.position, gizmoName + ".psd");    

}

// Called by the Unity Editor GUI every update cycle, but only when the object is selected. // Draws a sphere showing spawnRange's setting visually. function OnDrawGizmosSelected () { Gizmos.color = Color(0, 1, 1); Gizmos.DrawWireSphere(transform.position, spawnRange); }

What am I doing wrong? Thanks to those that have helped me.

Comment
Add comment · Show 1
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 31, 2011 at 11:37 PM 0
Share

I figured it out. I guess I wasn't applying it to the right prefab. I usually forget to apply a prefab. Well I found it out.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by sdfg · May 05, 2011 at 07:19 PM

i hump u hard im a guy

Comment
Add comment · 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

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

No one has followed this question yet.

Related Questions

Can't make enemy respawn 0 Answers

AI isn't looping (Javascript) 1 Answer

My Object only moves when the variable ready is true. Problem is that ready is only ever true while update is running. So to move the object the variable always has to be true. How can I make it move if the variable ready is false. 1 Answer

ERROR : QUACK is not a generic definition. What now? 1 Answer

Simple follow script issue 1 Answer


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