Search Unity

Particle effect upon firing

Discussion in 'Scripting' started by inaudiblefuzz, Jan 9, 2019.

  1. inaudiblefuzz

    inaudiblefuzz

    Joined:
    Nov 9, 2018
    Posts:
    12
    I would like my particle effect to fire once when I shoot and that's it. I think I got the code setup so that it ques when firing alright however the particles keep playing. I turned off looping and play on awake. Not sure what's causing it if it's in my code or the particle system. In this script I also have an impact effect with a fire rate set at .50f. Does the fire rate also effect how often the particle effect will go? I'm trying to get it so that when I "Fire" the dust comes out of the gun and the impact effect hits the wall instantly. .50 later I can fire again.



    Thank you!!




    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class Gun : MonoBehaviour
    4. {
    5.  
    6.     public float damage = 10f;                  //Damage
    7.     public float range = 500f;                  //Range of bullet
    8.     public float fireRate = .50f;               //Greater fire rate = less time between shots.
    9.     public float impactForce = 30f;             //Impact (velocity)
    10.     public ParticleSystem Muzzle;
    11.     public Camera fpsCam;                       //Camera. Click and drag off of hierarchy
    12.     public GameObject impactEffect;             //Prefab system for impact
    13.  
    14.     private float nextTimeToFire = 0f;          //Instant shot (no lag when pressing down)
    15.  
    16.     void Update()
    17.     {
    18.  
    19.         if (Input.GetButtonDown("Fire1") && Time.time >= nextTimeToFire)
    20.         {
    21.             nextTimeToFire = Time.time + 1f / fireRate;
    22.             Shoot();
    23.  
    24.         }
    25.  
    26.     }
    27.  
    28.     void Shoot()
    29.     {
    30.  
    31.         Muzzle.Play();                      //Play muzzle / barrel
    32.  
    33.         RaycastHit hit;
    34.         if (Physics.Raycast(fpsCam.transform.position, fpsCam.transform.forward, out hit, range))
    35.  
    36.         {
    37.          
    38.             Target target = hit.transform.GetComponent<Target>();
    39.             if (target != null)
    40.             {
    41.                 target.TakeDamage(damage);
    42.             }
    43.  
    44.             if (hit.rigidbody != null)             //Hit rigidbody component
    45.             {
    46.                 hit.rigidbody.AddForce(-hit.normal * impactForce);
    47.             }
    48.  
    49.             GameObject impactGO = Instantiate(impactEffect, hit.point, Quaternion.LookRotation(hit.normal));
    50.             Destroy(impactGO, .25f);  // Destroy impact effect
    51.             {
    52.              
    53.             }
    54.  
    55.         }
    56.     }
    57. }
     
    Last edited: Jan 9, 2019
  2. ACrimson

    ACrimson

    Joined:
    Mar 28, 2017
    Posts:
    30
    Try to destroy the impact in individual script and attach to the particle system prefab.