Hey! I’m an extremely new beginner, I only saw code for the first time about a week ago. I have learnt some things and I have got a pretty solid game project going on, and I currently have this double barreled shotgun in it. Currently the script shoots a single raycast forward, and I would like to have a good shotgun spread with around 24 pellets firing at once. I have read older posts on here about this, but as a beginner I’m a bit confused, and would like to know what to exactly do with my current script to give the weapon shotgun spread. Thanks in advance <3 !
using UnityEngine;
public class Gun : MonoBehaviour {
public float damage = 10f;
public float range = 100f;
private float TimeWhenAllowedNextShoot = 1.2f;
private float TimeBetweenShooting = 1.2f;
public float impactForce = 30f;
public Camera fpsCam;
public CameraShake cameraShake;
public ParticleSystem muzzleFlash;
public AudioSource shootingSound;
public GameObject impactEffect;
void Start()
{
shootingSound = GetComponent<AudioSource>();
}
void Update()
{
CheckIfShouldShoot();
}
void CheckIfShouldShoot()
{
if (TimeWhenAllowedNextShoot <= Time.time)
{
if (Input.GetMouseButton(0))
{
Shoot();
TimeWhenAllowedNextShoot = Time.time + TimeBetweenShooting;
}
}
}
void Shoot()
{
muzzleFlash.Play();
shootingSound.Play();
StartCoroutine(cameraShake.Shake(.4f, .5f));
RaycastHit hit;
if (Physics.Raycast(fpsCam.transform.position, fpsCam.transform.forward, out hit, range))
{
Debug.Log(hit.transform.name);
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, 2f);
}
}
}