Game Development Reference
In-Depth Information
Now, build a neat-looking enemy death particle using those rules. For our enemy
tanks, I will build an effect of red metal shards flying about. Store this new particle
emitter as a Prefab called
EnemyDeathFX
.
In
EnemyControllerScript
, add the following code at the beginning to store the
particle emitter:
public ParticleSystemdeathFxParticlePrefab = null;
At the bottom of
EnemyControllerScript
, modify
hitByPlayerBullet
to read as follows:
public void hitByPlayerBullet()
{
// Create the particle emitter object
GameObjectdeathFxParticle =
(GameObject)Instantiate(deathFxParticlePrefab);
// Get the enemy position
Vector3 enemyPos = transform.position;
// Create a new vector that is in front of the enemy
Vector3 particlePosition = new
Vector3(enemyPos.x,enemyPos.y,enemyPos.z + 1.0f);
// Reposition the particle emitter at this new position
deathFxParticle.transform.position = particlePosition;
Destroy(gameObject,0.1f);
}
Be sure to also assign the
EnemyDeathFX
Prefab to the
deathFxParticlePrefab
property of the
Enemy
object. Now, your enemies will display a particle effect
when defeated! However, the particle system we created is not getting destroyed
automatically. To wrap up this task, let's make the particle system destroy itself.
To do this, we will add a quick self-destruct script.
Create a new script called
SelfDestruct
, attach it to the
EnemyDeathFX
Prefab,
and code it up to look like the following:
using UnityEngine;
using System.Collections;
public class SelfDestruct : MonoBehaviour
{