Search Unity

2d Bullet hit particle

Discussion in '2D' started by abhishekluthra2005, Jun 5, 2020.

  1. abhishekluthra2005

    abhishekluthra2005

    Joined:
    Jun 2, 2020
    Posts:
    5
    I am new to programming and I am trying to make my tiny bullet hit particle system show when my cloned bullets collide with anything
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class bullet : MonoBehaviour
    6. {
    7.     //particle prefab
    8.     public GameObject impact;
    9.  
    10.     public float speed = 20f;
    11.     public int damage = 20;
    12.     public Rigidbody2D rb;
    13.  
    14.     // Start is called before the first frame update
    15.     void Start()
    16.     {
    17.         rb.velocity = transform.up * speed;
    18.     }
    19.  
    20.     private void OnTriggerEnter2D(Collider2D hitInfo)
    21.     {
    22.         enemyDamage enemy = hitInfo.GetComponent<enemyDamage>();
    23.  
    24.         //trying to clone the effect for each bullet
    25.         impact = Instantiate(impact, transform.position, Quaternion.identity) as GameObject;
    26.         if (enemy != null)
    27.         {
    28.             enemy.takeDamage(damage);
    29.         }
    30.         Destroy(gameObject);
    31.         Destroy(impact, .75f);
    32.     }
    33.  
    34.     // Update is called once per frame
    35.     void Update()
    36.     {
    37.        
    38.     }
    39. }
     
  2. abhishekluthra2005

    abhishekluthra2005

    Joined:
    Jun 2, 2020
    Posts:
    5
    Fixed this by instantiating my particle effect in a new method
    Code (CSharp):
    1. void impactPlay()
    2.     {
    3.         impact = Instantiate(impact, bulletEnd.transform.position, Quaternion.identity);
    4.         impact.GetComponent<ParticleSystem>().Play();
    5.         Destroy(impact, .8f);
    6.     }
     
  3. Lightning_A

    Lightning_A

    Joined:
    Nov 30, 2018
    Posts:
    7
    You don't need to do that, just check "Play on Awake" and set "Stop Action" to destroy in the particle system's settings
     
    Rilantes likes this.