Search Unity

Bullet Impact Effect Script

Discussion in 'Scripting' started by MikawasakiDigital, May 7, 2019.

  1. MikawasakiDigital

    MikawasakiDigital

    Joined:
    Mar 29, 2019
    Posts:
    95
    Hey guys, just want to say thanks to anyone who can offer there help.

    The situation here is on my shooting script, I'm able to make an impact effect using my Raycast, but it only affects my enemies? And once they get hit the effect lasts forever.

    What can I do to make the effects disappear, and so that it makes the effect on any and every surface?

    Bellow is a copy of my shooting script, and again thank you for any insight.
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class Carbine : MonoBehaviour
    4. {
    5.     public int minimumDamage = 5;
    6.  
    7.     public int maximumDamage = 15;
    8.  
    9.     public int maximumRange = 50;
    10.  
    11.     public float fireRate = .05f;
    12.  
    13.     private float nextFire = 0f;
    14.  
    15.     public ParticleSystem muzzleFlash;
    16.  
    17.     public Camera MainCamera;
    18.  
    19.     public GameObject impactEffect;
    20.  
    21.  
    22.  
    23.     void FixedUpdate ()
    24.     {
    25.         if (Input.GetButton("Fire1") && Time.time > nextFire)
    26.         {
    27.             Shoot();
    28.         }
    29.     }
    30.  
    31.     void Shoot ()
    32.     {
    33.         muzzleFlash.Play();
    34.  
    35.         RaycastHit hit;
    36.  
    37.         if (Physics.Raycast(MainCamera.transform.position, MainCamera.transform.forward, out hit, Mathf.Infinity))
    38.         {
    39.             IDamageable damageable = hit.collider.GetComponent<IDamageable>();
    40.  
    41.             if (damageable != null)
    42.             {
    43.                 float normalisedDistance = hit.distance / maximumRange;
    44.  
    45.                 if (normalisedDistance <= 1)
    46.                 {
    47.                     damageable.DealDamage(Mathf.RoundToInt(Mathf.Lerp(maximumDamage, minimumDamage, normalisedDistance)));
    48.  
    49.                     nextFire = Time.time + fireRate;
    50.                 }
    51.                 Instantiate(impactEffect, hit.point, Quaternion.LookRotation(hit.normal));
    52.             }
    53.         }
    54.     }
    55. }
     
  2. Move the line
    Code (CSharp):
    1. Instantiate(impactEffect, hit.point, Quaternion.LookRotation(hit.normal));
    outside of if (damageable != null), this way you will spawn impacteffect even if you don't hit "damageable" thing.
    But the surface needs to have collider, obviously.

    Also, if you do
    Code (CSharp):
    1. GameObject go = Instantiate<GameObject>(impactEffect, hit.point, Quaternion.LookRotation(hit.normal));
    2. Destroy(go, 2f);
    Then it will be destroyed after 2 seconds. See Destroy: https://docs.unity3d.com/ScriptReference/Object.Destroy.html

    (Later you will need to read up on object pooling, but for now it works).
     
    MikawasakiDigital likes this.
  3. MikawasakiDigital

    MikawasakiDigital

    Joined:
    Mar 29, 2019
    Posts:
    95
    Thanks for the help brother.

    I've only gone over pooling once, I'll take a dive into some articles asap.

    You're a blessing. :D

     
  4. mkeomkeo286

    mkeomkeo286

    Joined:
    Feb 9, 2021
    Posts:
    1
    thanks for the code dudes