Particle SYSTEM: SpaceShip On FIRE
SCRIPTING PARTICLE EFFECTS
Spaceship on Fire!
Objective: If the spaceship collides with asteroids, a fire ignites on its wings.
Since the player has 3 lives, One collision with asteroid will cause one fire, and the second fire on the second collision. On the third collision, spaceship will be destroyed.
Two particle systems: FireRightEngine, and FireLeftEngine, were created. The process of creating a fire particle effect is similar to how I create a flame in the previous post. In this section, I want to focus on how we can use scripting to actually play or pause the particle effect.
Parent-Child Relation
It is important to set a parent child relation between the gameobject that we want the particle effect on and the particle system. I have set the particles systems as child of the player and set the position where we would like the particle system to appear on the screen.
Play On Awake?
Another thing to remember is to uncheck the Play on Awake option in Particle System component. This will prevent particle effects from being played when the game started.
SCRIPTING
OBJECTIVE: Play fire effect on the right wing when asteroid hits the player first time. Then, play the effect on the second wing for the second hit. The third hit would destroy the spaceship. So that would be another section describing how we can destroy the object along with the destruction particle system.
PLAYERCONTROLLER SCRIPT
Although the particle system we create is put as a child of the player, the component is actually outside of the Player's component. So we need to set a variable in PlayerController.cs script that can cache the Particle System component. Similar to how we declare GameObjects, we can also declare ParticleSystem variables. Here, we have two fire particles, so let us create an Array.
ARRAY ELEMENTS REFERENCES
Now, lets go back to Unity editor and add elements into array we just created. I will add fireRightEngine as element 0 and fireLeftEngine as element 1.
DECISION MAKING
We have already scripted a condition where a collision of spaceship with asteroid reduces the playerLife by 1 amount. Lets use that variable "playerLife" to decide which particle system to play.
Condition 1: playerLife = 2 (i.e. First collision with asteroid)
play fireRightEngine, in our script --> fireParticle [0]
Condition 2: playerLife = 1 (i.e. Second collision with asteroid)
play fireLeftEngine, in our script --> fireParticle [1]
Condition 3: playerLife = 0 (i.e. Third collision with asteroid)
Game Over!
SCRIPT
With conditions set up above, we can now easily put it down in our script.
PROGRESS SO FAR