Search Unity

Raycast Bullet Spread

Discussion in 'Editor & General Support' started by Mr_AgentFox, Aug 6, 2019.

  1. Mr_AgentFox

    Mr_AgentFox

    Joined:
    Jun 23, 2019
    Posts:
    59
    Does anyone know how to create bullet spread with raycasting?

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using TMPro;
    4.  
    5. public class GunScript : MonoBehaviour
    6. {
    7.     public float reloadTime = 3f;
    8.     public float damage = 10f;
    9.     public float range = 100f;
    10.     public float impactForce = 10000f;
    11.     public float fireRate = 15f;
    12.     public int maxAmmo = 100;
    13.     public float bulletSpreadAmount = 1f;
    14.     private int currentAmmo;
    15.     public bool isReloading = false;
    16.  
    17.  
    18.     public Camera fpsCam;
    19.     public TextMeshProUGUI ammoTxt;
    20.     public ParticleSystem muzzleFlash;
    21.     public GameObject impactEffect;
    22.     public WeaponSwitcher switchWeapon;
    23.     public Animator animator;
    24.  
    25.     private float nextTimeToFire = 0f;
    26.  
    27.     void Start()
    28.     {
    29.         currentAmmo = maxAmmo;
    30.     }
    31.  
    32.     void OnEnable()
    33.     {
    34.         isReloading = false;
    35.         animator.SetBool("isReloading", false);
    36.     }
    37.  
    38.     // Update is called once per frame
    39.     void Update()
    40.     {
    41.         if (isReloading)
    42.         {
    43.             return;
    44.         }
    45.  
    46.         if (switchWeapon.isSwitching)
    47.         {
    48.             return;
    49.         }
    50.  
    51.         if (currentAmmo <= 0)
    52.         {
    53.             StartCoroutine(Reload());
    54.             return;
    55.         }
    56.  
    57.         ammoTxt.text = gameObject.GetComponent<GunScript>().currentAmmo.ToString();
    58.  
    59.         if (Input.GetButton("Fire1") && Time.time >= nextTimeToFire)
    60.         {
    61.             nextTimeToFire = Time.time + 1f / fireRate;
    62.             Shoot();
    63.         }
    64.     }
    65.  
    66.     public void Shoot()
    67.     {
    68.         muzzleFlash.Play();
    69.  
    70.         currentAmmo--;
    71.  
    72.         RaycastHit hit;
    73.         if (Physics.Raycast(fpsCam.transform.position, fpsCam.transform.forward, out hit, range))
    74.         {
    75.  
    76.             Target target = hit.transform.GetComponent<Target>();
    77.             if (target != null)
    78.             {
    79.                 target.TakeDamage(damage);
    80.             }
    81.  
    82.             Ragdoll target1 = hit.transform.GetComponent<Ragdoll>();
    83.             if (target1 != null)
    84.             {
    85.                 target1.TakeDamage(damage);
    86.             }
    87.  
    88.             RagdollNotMoving target2 = hit.transform.GetComponent<RagdollNotMoving>();
    89.             if (target2 != null)
    90.             {
    91.                 target2.TakeDamage(damage);
    92.             }
    93.  
    94.             if (hit.rigidbody != null)
    95.             {
    96.                 hit.rigidbody.AddForceAtPosition(fpsCam.transform.forward * impactForce, hit.point);
    97.             }
    98.  
    99.             GameObject impaceGO = Instantiate(impactEffect, hit.point, Quaternion.LookRotation(hit.normal));
    100.             Destroy(impaceGO, 1f);
    101.         }
    102.     }
    103.  
    104.     IEnumerator Reload()
    105.     {
    106.         isReloading = true;
    107.         Debug.Log("Reloading...");
    108.  
    109.         animator.SetBool("isReloading", true);
    110.  
    111.         yield return new WaitForSeconds(reloadTime - .25f);
    112.         animator.SetBool("isReloading", false);
    113.         yield return new WaitForSeconds(.25f);
    114.  
    115.         currentAmmo = maxAmmo;
    116.         isReloading = false;
    117.     }
    118. }
    119.  
     
  2. Reedex

    Reedex

    Joined:
    Sep 20, 2016
    Posts:
    389
    Code (CSharp):
    1. Vector3 fwd = fpsCam.transform.forward;
    2. fwd = fwd + fpsCam.TransformDirection (new Vector3(Random.Range(-bulletSpreadAmount , bulletSpreadAmount ),Random.Range(-bulletSpreadAmount , bulletSpreadAmount )));
    3.         if (Physics.Raycast (fpsCam.transform.position, fwd, out hit,range))
    4.         {
    5.             //etc
    6.         }
    7.  
    btw i see that you post this question in general support, i guess you'd get more attention in the scripting forum...
     
  3. Mr_AgentFox

    Mr_AgentFox

    Joined:
    Jun 23, 2019
    Posts:
    59

    It worked! Thank you for your help. And I didn't even realize I was in general support, oops.
     
  4. gruella_779

    gruella_779

    Joined:
    Oct 3, 2020
    Posts:
    3
    It worked for me aswell! Is there any way I can modify a crosshair to represent the spread, btw?
     
  5. gruella_779

    gruella_779

    Joined:
    Oct 3, 2020
    Posts:
    3
    I forgot to reply, there's a small mistake.
    In the fwd = .... line, fpsCam.TransformDirection is written, when it must be fpsCam.transform.TransformDirection.
     
  6. The_Tornado22

    The_Tornado22

    Joined:
    Sep 6, 2020
    Posts:
    1
    i was confused at first but this helped thanks