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.
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!
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