Instantiating a particle system at runtime

In this recipe, we take care of instantiating a particle system prefab in the place of a collected item that disappears, and also use the particle system to play the sound effect we set for this specific game event.

To achieve this, we need a new script and need to make some modifications to the Runner script, so let's begin.

Getting ready

Keep your project ready to add the new assets we need.

How to do it

  1. Create a new C# script in the Scripts folder of your project and name it PS_Manager.
  2. Edit the Update() function so that it contains the following lines. They tell the PS to destroy itself once the audio attached to it has finished playing:
    void Update () {
      if(!audio.isPlaying){
        Destroy(this.gameObject);
      }
    }
  3. Now open the Runner script in Monodevelop, if you did not open before.
  4. To begin with, we need a public GameObject variable to store the reference to the particle system prefab we are going to create in the scene. Add the following line to the script:
    public GameObject ps;
  5. Add this line to the collectible's if() statement inside the OnCollisionEnter() function. It takes care of creating the PS in the place of the collectible, which can then be destroyed:
    Instantiate(ps, c.transform.position, Quaternion.identity);
  6. For extra clarity, we are displaying the complete, updated OnCollisionEnter() function of the Runner script:
    void OnCollisionEnter(Collision c){
      
      if(c.gameObject.tag == "platform"){
        bIsTouch = true;
        charAnimator.SetBool("bJump",false);
      }
      if(c.gameObject.tag == "collectible"){
          Instantiate(ps, c.transform.position, Quaternion.identity);
          GameObject.Destroy(c.gameObject);
          collected += 1;
        }
      }
  7. Save both Runner and PS_Manager in Monodevelop.
  8. Drag PS_Manager from the Project panel onto the PS on the scene.
  9. Now create a new prefab in the Prefabs folder and name it coll_ps_prefab. Then drag the particle system from the scene onto coll_ps_prefab in the Project panel. With that done, the particle system in the scene can be removed.
  10. The last step is to select the runner character in Scene to access its Runner script in the Inspector panel, and from there, drag coll_ps_prefab into the Ps slot we created, with the ps public variable.

    You can refer to the following screenshot that shows the operation we just described:

    How to do it

How it works...

Using the particle system, we solved our problem of playing an audio clip on a game object that is about to be destroyed. Instead of putting the audio clip in the collectible item, we put it in a particle system prefab that we instantiate in the scene whenever a collectible is gathered by the player.

At the same time, we provide clear, consistent feedback to the player that they achieve something good when the character hits a collectible.

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

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