Technology Sharing

[Unity2D 2022: Particle System] Add hit particle effects

2024-07-12

한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina

1. Create particle effects game objects

2. Modify particle system properties

1. Basic attributes

(1) Change the duration of emitted particles to 1s

(2) Uncheck Looping

(3) Modify the particle life time (Start Lifetime) to a random value between 0.1 and 0.2

(4) Change the particle initial speed (Start Speed) to 0

(5) Change the particle's Start Size to a random value between 0.7 and 1.

(6) Change the particle's initial rotation angle (Start Rotation) to a random value between 0 and 360

2. Emission

(1) Set the emission particle speed (Rate over Time) to 0 (i.e. no particles are emitted)

(2) Add a Burst, with a Time of 0 and a Count of 5

3. Emitter Shape

(1) Set the Shape to Circle

(2) Set the rotation angle (Rotation) in the x-axis direction to 900

(3) Set the radius to 0.12

(4) Set the Mode to Burst Spread

4. Particle Color (Color over Lifetime)

(1) Set the transparency to 255-0 (i.e., increasingly transparent)

5. Particle size (Size over Lifetime)

(1) Set the particle size to 0.6-1

6. Texture Sheet Animation

(1) Add particle sprite image

3. Create a hit effect prefab

4. Play special effects when bullets hit enemies

1. Edit the bullet script:

(1) Create a hit effect prefab

  1. public class Bullet : MonoBehaviour
  2. {
  3. // 创建命中特效预制体
  4. public GameObject hitEffectParticlePrefab;
  5. }

(2) Rewrite the iterator interface and delete the hit particle effects after a delay of 1s

  1. public class Bullet : MonoBehaviour
  2. {
  3. // 创建命中特效预制体
  4. public GameObject hitEffectParticlePrefab;
  5. // 在1s后删除粒子特效
  6. private async Task deleteEffectParticle(GameObject EffectParticle, float delay)
  7. {
  8. // 等待1s
  9. await Task.Delay(1000);
  10. // 删除粒子特效
  11. Destroy(EffectParticle);
  12. }
  13. }

(3) When hitting an enemy, a hit particle effect is created at the bullet's location and deleted after 1 second.

  1. public class Bullet : MonoBehaviour
  2. {
  3. // 创建命中特效预制体
  4. public GameObject hitEffectParticlePrefab;
  5. // 击中敌人
  6. private void OnCollisionEnter2D(Collision2D collision)
  7. {
  8. Enemy enemy = collision.gameObject.GetComponent<Enemy>();
  9. if(enemy != null) {
  10. enemy.changeHealthPoint(-25);
  11. GameObject hitEffectParticle = Instantiate(hitEffectParticlePrefab, transform.position, Quaternion.identity);
  12. deleteHitEffectParticle(hitEffectParticle, 1);
  13. }
  14. Destroy(gameObject);
  15. }
  16. // 在1s后删除粒子特效
  17. public static async Task deleteEffectParticle(GameObject EffectParticle, float delay)
  18. {
  19. // 等待1s
  20. await Task.Delay(1000);
  21. // 删除粒子特效
  22. Destroy(EffectParticle);
  23. }
  24. }

2. Add hit particle effects to the bullet prefab

3. The final effect is shown in the figure below:

This chapter is over. Thanks for reading!