Time for action – Detecting collisions

  1. Import the Target Dummy model from the project assets folder.
  2. To ensure that your game objects have collision data, be sure to check the Generate Colliders setting in the Import Settings for your mesh.

    Note

    In many cases, however, it is less costly and reasonably accurate to use a simple sphere or box collider for your enemies. If you want to do location specific damage, this is one of the cheaper ways to accomplish the effect on mobile devices.

    Time for action – Detecting collisions

  3. Now that we have collision data for our target game objects, we need to add a particle collider to the particle system we created earlier. We do this by selecting the component in the Component | Particles | World Particle Collider menu.
    Time for action – Detecting collisions

    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.

  4. To do this we select the Send Collision Message in the World Particle Collider so that each particle game object and the game object involved in the collision receive a collision message.
    Time for action – Detecting collisions

    We can look for this event in scripts that we attach to any object we want to receive collision events from the particle system.

  5. Create a script called 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!");
       }   
     
    }
  6. Update the 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.

  7. Attach the 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.

What just happened?

We have now handled the other part of our gameplay. The player can fire a weapon, the projectiles will be emitted using a particle system and when the particles come into contact with another object the damage can be calculated and this object removed from the game.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.253.62