Search Unity

New to particle effects. Trying to make a rail

Discussion in 'Editor & General Support' started by inaudiblefuzz, Jan 10, 2019.

  1. inaudiblefuzz

    inaudiblefuzz

    Joined:
    Nov 9, 2018
    Posts:
    12
    Hello all,

    I'm trying to make a rail gun. I have a gun script that deals damage, fire rate etc.

    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. }
    58.  
    59.  
    60.  
    I would like to create a particle effect laser that also fires. I'm not sure how to go about that. I've tried a few tutorials but they have all been stationary turrets. Or tutorials on how to make particles but not how to fire them out of a gun.

    I'm a new user so I'll take any / all the help I can get! I tried various things on my own today to no avail. My gun script has a Muzzle (particle system) slot. Would it be possible to fire a straight line / projectile from there? What's the best / easiest way to accomplish this?

    Thank you.