How to Spread Raycast (For Shotgun)
Im trying to make Spread Shoot for my shotgun Script can you help me? using UnityEngine; using System.Collections; public class ShotGun : MonoBehaviour { public float Damage = 10f; public float Range = 100f; public float FireRate = 0.5f; public float impactForce = 70f; public AudioSource GunSound; public AudioClip ReloadSound; public int MaxAmmo = 5; private bool Shooting = false; private int currentAmmo = -1; public float reloadTime = 1f; private bool isReloading = false; public Camera fpsCam; public ParticleSystem muzzleFlash; public GameObject ImpactEffect; private float nextTimeToFire = 0f; public Animator animator; void Start() { GunSound = GetComponent<AudioSource>(); if (currentAmmo == -1) currentAmmo = MaxAmmo; } void OnEnable() { isReloading = false; animator.SetBool("Reloading...", false); } void Update() { if (isReloading) return; if (currentAmmo <= 0) { StartCoroutine(Reload()); return; } if (Input.GetButtonDown("Fire1") && Time.time >= nextTimeToFire) { nextTimeToFire = Time.time + 1f / FireRate; GunSound.Play(); Shoot(); StartCoroutine(ShootingS()); } } IEnumerator ShootingS() { Shooting = true; animator.SetBool("Sniper", true); yield return new WaitForSeconds(.1f); animator.SetBool("Sniper", false); } IEnumerator Reload() { isReloading = true; Debug.Log("Reloading..."); gameObject.GetComponent<AudioSource>().PlayOneShot(ReloadSound); animator.SetBool("Reloading...", true); yield return new WaitForSeconds(reloadTime - .25f); currentAmmo = MaxAmmo; yield return new WaitForSeconds(.25f); animator.SetBool("Reloading...", false); isReloading = false; } void Shoot() { muzzleFlash.Play(); currentAmmo--; RaycastHit hit; if (Physics.Raycast(fpsCam.transform.position, fpsCam.transform.forward, out hit, Range)) { Target target = hit.transform.GetComponent<Target>(); if (target != null) { target.TakeDamage(Damage); } if (hit.rigidbody != null) { hit.rigidbody.AddForce(-hit.normal * impactForce); } GameObject impactGO = Instantiate(ImpactEffect, hit.point, Quaternion.LookRotation(hit.normal)); Destroy(impactGO, 1.5f); } } }
How to make it Spread? @AT-Brackeys , Help Anyone?
Your answer
![](https://koobas.hobune.stream/wayback/20220612225245im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Really need some help with my bullets firing opposite and not following rotation. 0 Answers
Test if a bullet will hit before shooting it? 2 Answers
Raycast not detecting hit with collider 0 Answers
How can I create Raycast bullet innaccuracy as a cone? 2 Answers
How do you make bullet Spread? 0 Answers