Raycast shotgun help!
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);
}
}
}
Answer by MKayJay · Sep 21, 2021 at 02:46 PM
Hey. Have you considered something like a 'Cone Cast'? Although it's not something directly in Unity, a quick google search led me to a GitHub page, where there's an extension to the Physics class that does exactly that. It also comes with a small example of how to use it, maybe it will be helpful?
https://github.com/walterellisfun/ConeCast
Just to emphasize, I did not make this and do not take credit for it, I simply found it by googling a bit. (Sometimes half the task is figuring out what to google)
I figured out something using a cone would give the results I'm looking for. That being said, I really have no much idea how to implement such a thing into the weapon script. Still a very new newbie.
Answer by AlgoUnity · Sep 22, 2021 at 10:17 AM
One raycast is like shooting one bullet, your shotgun shoots 24 pellets. So you want to do 24 raycasts in a cone formation. You can use a for-loop to repeat the raycast 24 times for this (google it if you don't know what that is). Right now you are casting a ray straight out from the camera to the center of the screen. By changing the fpsCam.transform.forward part of your Physics.Raycast() function, you can change the direction that the ray goes. There's a lot of different ways to do it, some are better but more complicated, some are worse but a lot simpler. For an easy but bad example, you can do (fpsCam.transform.forward + fpsCam.transform.right * 0.1f) to aim the bullet slightly to the right, change 0.1f to make it go more or less to the right (negative values for left). More ideal would be to use Quaternion.RotateTowards() but that takes more understanding of transforms, local space, world space, etc. I would start with just copy pasting like 2 or 3 more Raycast code blocks like the one you have underneath, and changing the direction for each of them, to keep it simple in your case until you understand it better, the for-loop math can come later.