By itself this will result in particles colliding with other objects, but what we really want is for the game objects to be notified when the collision happens so we can have our target react to being hit.
We can look for this event in scripts that we attach to any object we want to receive collision events from the particle system.
Damage
and attach the collision script to the Test Dummy objects that will process the damage. using UnityEngine; using System.Collections; public class Damage : MonoBehaviour { void OnParticleCollision() { Debug.Log("Hit!"); } }
OnParticleCollision()
method to update the health of the object that has been hit by the particle. When the health of that item reaches zero, remove the test dummy from the game.void OnParticleCollision() { HealthScript healthScript = GetComponent<HealthScript>(); healthScript.takeHit(1); Debug.Log("Hit!"); }
Here we are going to have our Damage script communicate with a HealthScript
, which will define the health of our target dummy objects and remove them when they have run out of health.
HealthScript
to the target dummies.using UnityEngine; using System.Collections; public class HealthScript : MonoBehaviour { public int initialHealth; private int currentHealth; // Use this for initialization void Start () { currentHealth = initialHealth; } public void TakeHit( int damage ) { currentHealth = currentHealth - damage; if ( currentHealth <= 0 ) { Destroy( this.gameObject ); } } }
Here we will simply remove the game object after it has taken enough damage by telling the game object that this script is attached to to destroy itself. If we wanted this object to explode or take some other behavior we could Instantiate an explosion prefab at this game object's location when we destroy the original object.
18.191.253.62