• 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 arrowhead896 · Jun 18, 2012 at 04:39 PM · spawnlerpzrobot

Lerpz Escapes: CopperSpawner doesn't remove dead robots

I can't figure out how to get the dead robots to disappear when the new one spawns. Can anyone help?

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Berenger · Jun 18, 2012 at 10:16 PM

 Destroy( robot.gameObject ); // Or whatever the robot's ref is called
Comment
Add comment · Show 4 · 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 arrowhead896 · Jun 18, 2012 at 11:55 PM 0
Share

The problem is that I'm not sure how to deter$$anonymous$$e whether the robot is dead or alive. The fundamental issue seems to be that the robot prefab generates a new prefab for the dead robot, while destroying itself, and the script then doesn't have a way to access the robot.

avatar image Berenger · Jun 19, 2012 at 12:00 AM 0
Share

Could you give us some of the codes used to create a new robots, or the death etc. ?

avatar image arrowhead896 · Jun 22, 2012 at 10:43 PM 0
Share

$$anonymous$$illing the robot:

var hitPoints = 3;

var explosionPrefab : Transform; var dead$$anonymous$$odelPrefab : Transform; var healthPrefab : Droppable$$anonymous$$over; var fuelPrefab : Droppable$$anonymous$$over; var drop$$anonymous$$in = 0; var drop$$anonymous$$ax = 0;

// sound clips: var struckSound : AudioClip;

private var dead = false;

function ApplyDamage (damage : int) { // we've been hit, so play the 'struck' sound. This should be a metallic 'clang'. if (audio && struckSound) audio.PlayOneShot(struckSound);

 if (hitPoints <= 0)
     return;

 hitPoints -= damage;
 if (!dead && hitPoints <= 0)
 {
     Die();
     dead = true;
 }

}

function Die () {
// $$anonymous$$ill ourselves Destroy(gameObject);

 // Instantiate replacement dead character model
 var dead$$anonymous$$odel = Instantiate(dead$$anonymous$$odelPrefab, transform.position, transform.rotation);
 CopyTransformsRecurse(transform, dead$$anonymous$$odel);
 
 // create an effect to let the player know he beat the enemy
 var effect : Transform = Instantiate(explosionPrefab, transform.position, transform.rotation);
 effect.parent = dead$$anonymous$$odel;
 
 // fall away from the player, and spin like a top
 var dead$$anonymous$$odelRigidbody = dead$$anonymous$$odel.rigidbody;
 var relativePlayerPosition = transform.InverseTransformPoint(Camera.main.transform.position);
 dead$$anonymous$$odelRigidbody.AddTorque(Vector3.up * 7);
 if (relativePlayerPosition.z > 0)
     dead$$anonymous$$odelRigidbody.AddForceAtPosition(-transform.forward * 2, transform.position + (transform.up * 5), Force$$anonymous$$ode.Impulse);
 else
     dead$$anonymous$$odelRigidbody.AddForceAtPosition(transform.forward * 2, transform.position + (transform.up * 2), Force$$anonymous$$ode.Impulse);
 
 // drop a random number of pickups in a random fashion
 var toDrop = Random.Range(drop$$anonymous$$in, drop$$anonymous$$ax + 1);    // how many shall we drop?
 for (var i=0;i<toDrop;i++)
 {
     direction = Random.onUnitSphere;    // pick a random direction to throw the pickup.
     if(direction.y < 0)
         direction.y = -direction.y;    // make sure the pickup isn't thrown downwards
     
     // initial position of the pickup
     var dropPosition = transform.TransformPoint(Vector3.up * 1.5) + (direction / 2);
 
     var dropped : Droppable$$anonymous$$over;

     // select a pickup type at random
     if(Random.value > 0.5)
         dropped = Instantiate(healthPrefab, dropPosition, Quaternion.identity);
     else
         dropped = Instantiate(fuelPrefab, dropPosition, Quaternion.identity);

     // set the pickup in motion
     dropped.Bounce(direction * 4 * (Random.value + 0.2));
 }

}

/ dead$$anonymous$$odel When we instantiate the death sequence prefab, we ensure all its child elements are the same as those in the original robot, by copying all the transforms over. Hence this function. /

static function CopyTransformsRecurse (src : Transform, dst : Transform) { dst.position = src.position; dst.rotation = src.rotation;

 for (var child : Transform in dst)
 {
     // $$anonymous$$atch the transform with the same name
     var curSrc = src.Find(child.name);
     if (curSrc)
         CopyTransformsRecurse(curSrc, child);
 }

}

@script AddComponent$$anonymous$$enu("Third Person Enemies/Enemy Damage")

avatar image arrowhead896 · Jun 22, 2012 at 10:43 PM 0
Share

Spawning a new one:

/* EnemyRespawn.js

 This script checks if the player is in range. If so, it instantiates the enemy prefab specified. When the player moves out of range, the prefab is automatically destroyed.
 
 This prevents repeated calls to the enemy's AI scripts when the AI is nowhere near the player, this improving performance.

*/

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); }

avatar image
0

Answer by wiambt · Jun 22, 2012 at 02:36 PM

go to ennemydamage javescript and let this line of code the last one in the Die() function:

Destroy(deadModel.gameObject, 2);

Comment
Add comment · Show 1 · 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 arrowhead896 · Jun 22, 2012 at 10:44 PM 0
Share

No, that destroys it instantly. I want to destroy the dead model as the new one spawns.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Make lerpz invisible to enemies by hitting fire 2? A cloak? 1 Answer

Sounds of popping and slurping in Lerpz tutorial after Respawn section implemented 1 Answer

Where can I see Lerpz's Animations? 1 Answer

I cant Control Lerpz with the Control Keys 1 Answer

Spawn different Player models 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