- Home /
Making projectiles disappear when they hit the ground
I've got this prototype game where the player throws apples at zombies. The problem is I haven't worked out a good bit of code to destroy the apples upon contact with the ground.
http://www.mediafire.com/?wgkqdvfoaqn73ew
Alternatively, it would be great if the apples could de-spawn after a period of say, 2 seconds. Would each apple needs its own time variable? Many thanks.
Thank you everyone. I managed to solve my problem!
Answer by skalev · Sep 11, 2012 at 03:39 AM
Put a collider on the floor.
Have a script on the apple, and OnCollisionEnter() yield for how long you want to wait. Then call destroyObject.
Tried that but for some reason the apples stop spawning. $$anonymous$$aybe it's because it's firing from within an object. $$anonymous$$aybe if I position the apple away from the character as its fired.
if u have an instantiate code...
dont use destroy(gameObject) ins$$anonymous$$d use DESTROY(gameObject.Find("YOUR OBJECT(clone)");
if u use destroy(gameObject).. it will have errors because there is no more prefab to instantiate..
else if u use destroy(gameObject.Find("your object(clone)").. it only destroy the clone..
Doesn't seem to work. Prefab name Apple2b, code as follows:
Destroy(gameObject.Find("Apple2b(clone)"));
Ins$$anonymous$$d of using yield and then Destroy, you can use the 2nd parameter of Destroy.
Destroy("object", 2);
Would destroy in 2s.
Answer by MickM · Sep 11, 2012 at 04:59 AM
I did this pretty easily with a bow and arrow game; you obviously have it working however to put in in perspective (in case you do projectile stuff differently) I had a script attached to the bow/person that handles the firing (ie. instantating the prefab) and the prefab has its own script attached to it.
The prefab script is where you handle the despawn stuff - you can de-spawn your projectile by using
Destroy (gameObject)
in the script. If you want to de-spawn it after it hits something you put the de-spawn command inside
function OnCollisionEnter(collision : Collision){
}
so an example - you want to despawn it 5 seconds after hitting something - make a script called AppleFlight.js (for example) with the following code:
var despawnTime = 5;
function OnCollisionEnter(collision : Collision){
yield WaitForSeconds(despawnTime);
Destroy(gameObject);
}
Then drag this script onto your apple prefab - should work as planned! Hope it helps!!!
Also:
For info you can use the collision for other stuff as well - the 'collision' variable is what it collided with - eg. my arrow script i wanted the arrow to stick into something if it collided with it (including moving targets) so i added the following into the OnCollisionEnter function :
transform.parent = collision.transform; //The arrow parent is now what it collided with
rigidbody.isKinematic = false; //Freeze position of arrow
rigidbody.collider.enabled = false; //turn off collider
Tried that but after that amount of time all the apples disappear and spawning new apples stops working. How do I get the despawnTime to work for each individual apple, and not all of them?
Where have you attached the scripts? And what is in the projectile script?