Calling the Damage Particle System from Code

Now that we have our spray of stars looking right, we need to hook it into the code so that it fires when the player takes damage. We’ll add some code to our PlayerStats script to take care of that now.

Add the variables shown in Listing 11.1 and update the PlayHitReaction() function as shown.

Listing 11.1 Changes to the PlayerStats Script

public ParticleSystem particleHitLeft;public ParticleSystem particleHitRight;void PlayHitReaction(){  this.isImmune = true;  this.immunityTime = 0f;  PlayerController playerController =this.gameObject.GetComponent<PlayerController>();  if(playerController.isFacingRight == true)  {    this.particleHitLeft.Play();  }  else  {    this.particleHitRight.Play(); ...

Get Learning 2D Game Development with Unity®: A Hands-On Guide to Game Creation now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.