So, I’m having a bit of trouble with my shotgun, I don’t know how to offset it nor make multiple rays for the spread. Would anybody mind taking a look at it?
This is just the raycasting portion:
void ShotgunFire () {
RaycastHit hit;
float range = Mathf.Infinity;
bool firing = Physics.Raycast(transform.position, transform.forward, out hit, range);
GameObject thing = hit.collider.gameObject;
gunObj.transform.Translate(Vector3.forward * recoil);
audio.PlayOneShot(gunshot);
if(firing){
if(thing.tag == "Untagged"){
Instantiate(debrisParticle, hit.point, Quaternion.LookRotation(hit.normal));
}
if(thing.tag == "Player"){
Instantiate(bloodParticle, hit.point, Quaternion.LookRotation(hit.normal));
}
}
}
This will give you a shot with a random spread radius. I haven’t compile the code so there might be an error.
var shotCount = 10;
var spreadRadius = 0.5f;
var gunForward = transform.forward;
var gunPos = transform.position;
var cachedTrans = transform;
for(int i = 0; i < shotCount; i++)
{
//Grab random vector
var bulletVec = gunForward +
cachedTrans.TransformDirection(
new Vector3(
Random.Range(-spreadRadius, spreadRadius),
Random.Range(-spreadRadius, spreadRadius),
0));
RaycastHit hit;
float range = Mathf.Infinity;
bool wasHit = Physics.Raycast(gunPos, bulletVec, out hit, range);
// check this ray
if(!wasHit)
{
continue;
}
GameObject thing = hit.collider.gameObject;
gunObj.transform.Translate(Vector3.forward * recoil);
audio.PlayOneShot(gunshot);
if(firing)
{
if(thing.tag == "Untagged")
{
Instantiate(debrisParticle, hit.point, Quaternion.LookRotation(hit.normal));
}
if(thing.tag == "Player")
{
Instantiate(bloodParticle, hit.point, Quaternion.LookRotation(hit.normal));
}
}
}