I’m playing around with it, trying to use an array of Rays and then a foreach loop but can’t quite work it out:
var shots = new Ray[5];
shots[0] = fpsCam.ViewportPointToRay(new Vector3(0.5f, 0.5f, 0.0f));
shots[1] = fpsCam.ViewportPointToRay(new Vector3(0.5f, 0.55f, 0.0f));
shots[2] = fpsCam.ViewportPointToRay(new Vector3(0.5f, 0.45f, 0.0f));
shots[3] = fpsCam.ViewportPointToRay(new Vector3(0.53f, 0.5f, 0.0f));
shots[4] = fpsCam.ViewportPointToRay(new Vector3(0.47f, 0.5f, 0.0f));
Ray currentShot = shots[0];
Ray rayOrigin = fpsCam.ViewportPointToRay(new Vector3(0.5f, 0.5f, 0.0f));
Ray rayOrigin1 = fpsCam.ViewportPointToRay(new Vector3(0.5f, 0.55f, 0.0f));
Ray rayOrigin2 = fpsCam.ViewportPointToRay(new Vector3(0.5f, 0.45f, 0.0f));
Ray rayOrigin3 = fpsCam.ViewportPointToRay(new Vector3(0.53f, 0.5f, 0.0f));
Ray rayOrigin4 = fpsCam.ViewportPointToRay(new Vector3(0.47f, 0.5f, 0.0f));
foreach (var shot in shots)
{
if (Physics.Raycast(shots[0], out hit, Mathf.Infinity))
{
// if the object hit has as target script attached, run the take damage function
Target target = hit.transform.GetComponent<Target>();
if (target != null)
{
target.takeDamage(shotgunDamage);
}
GameObject impactEffectInstance = Instantiate(impactEffect, hit.point, Quaternion.LookRotation(hit.normal));
Destroy(impactEffectInstance, 1f);
}
I don’t think it’ll work like that. Also trying with a function as you suggested like this:
void shotgunShot(Ray rayOrigin, RaycastHit hit)
{
}
But again I can’t quite work out how to implement it, i.e. how to pass the Ray and RaycastHit into the function
Sorry I’m quite new to this! (as you can probably tell)
Here’s my full script minus the stuff I’m testing, maybe you can let me know what you think 
using System.Collections.Generic;
using UnityEngine;
public class Shotgun : MonoBehaviour
{
public float fireRate = 1.0f;
private float nextFire;
public int shotgunForce = 50;
public int shotgunDamage = 10;
public Transform gunEnd;
public Camera fpsCam;
public ParticleSystem muzzleFlash;
public GameObject impactEffect;
void Update()
{
// Check if the player has pressed the fire button and if enough time has elapsed since they last fired
if (Input.GetKey(KeyCode.Mouse0) && Time.time > nextFire)
{
// Update the time when our player can fire next
nextFire = Time.time + fireRate;
PlayerFireWeapon();
}
if (Input.GetKey(KeyCode.Mouse1))
PlayerFireAlternative();
}
void PlayerFireWeapon()
{
FindObjectOfType<AudioManager>().Play("ShotgunFire");
muzzleFlash.Play();
// Declare a raycast hit to store information about what our raycast has hit
RaycastHit hit;
RaycastHit hit1;
RaycastHit hit2;
RaycastHit hit3;
RaycastHit hit4;
Ray rayOrigin = fpsCam.ViewportPointToRay(new Vector3(0.5f, 0.5f, 0.0f));
Ray rayOrigin1 = fpsCam.ViewportPointToRay(new Vector3(0.5f, 0.55f, 0.0f));
Ray rayOrigin2 = fpsCam.ViewportPointToRay(new Vector3(0.5f, 0.45f, 0.0f));
Ray rayOrigin3 = fpsCam.ViewportPointToRay(new Vector3(0.53f, 0.5f, 0.0f));
Ray rayOrigin4 = fpsCam.ViewportPointToRay(new Vector3(0.47f, 0.5f, 0.0f));
/* ------------------FIRST SHOT--------------------- */
// Check if our raycast has hit anything
if (Physics.Raycast(rayOrigin, out hit, Mathf.Infinity))
{
// if the object hit has as target script attached, run the take damage function
Target target = hit.transform.GetComponent<Target>();
if (target != null)
{
target.takeDamage(shotgunDamage);
}
GameObject impactEffectInstance = Instantiate(impactEffect, hit.point, Quaternion.LookRotation(hit.normal));
Destroy(impactEffectInstance, 1f);
}
// Check if the object we hit has a rigidbody attached
if (hit.rigidbody != null)
{
// Add force to the rigidbody we hit, in the direction from which it was hit
hit.rigidbody.AddForce(-hit.normal * shotgunForce);
}
/* ------------------SECOND SHOT--------------------- */
// Check if our raycast has hit anything
if (Physics.Raycast(rayOrigin1, out hit1, Mathf.Infinity))
{
// if the object hit has as target script attached, run the take damage function
Target target = hit1.transform.GetComponent<Target>();
if (target != null)
{
target.takeDamage(shotgunDamage);
}
GameObject impactEffectInstance = Instantiate(impactEffect, hit1.point, Quaternion.LookRotation(hit1.normal));
Destroy(impactEffectInstance, 1f);
}
// Check if the object we hit has a rigidbody attached
if (hit1.rigidbody != null)
{
// Add force to the rigidbody we hit, in the direction from which it was hit
hit1.rigidbody.AddForce(-hit1.normal * shotgunForce);
}
/* ------------------THIRD SHOT---------------------*/
// Check if our raycast has hit anything
if (Physics.Raycast(rayOrigin2, out hit2, Mathf.Infinity))
{
// if the object hit has as target script attached, run the take damage function
Target target = hit2.transform.GetComponent<Target>();
if (target != null)
{
target.takeDamage(shotgunDamage);
}
GameObject impactEffectInstance = Instantiate(impactEffect, hit2.point, Quaternion.LookRotation(hit2.normal));
Destroy(impactEffectInstance, 1f);
}
// Check if the object we hit has a rigidbody attached
if (hit2.rigidbody != null)
{
// Add force to the rigidbody we hit, in the direction from which it was hit
hit2.rigidbody.AddForce(-hit2.normal * shotgunForce);
}
/* ------------------FOURTH SHOT---------------------*/
// Check if our raycast has hit anything
if (Physics.Raycast(rayOrigin3, out hit3, Mathf.Infinity))
{
// if the object hit has as target script attached, run the take damage function
Target target = hit3.transform.GetComponent<Target>();
if (target != null)
{
target.takeDamage(shotgunDamage);
}
GameObject impactEffectInstance = Instantiate(impactEffect, hit3.point, Quaternion.LookRotation(hit3.normal));
Destroy(impactEffectInstance, 1f);
}
// Check if the object we hit has a rigidbody attached
if (hit3.rigidbody != null)
{
// Add force to the rigidbody we hit, in the direction from which it was hit
hit3.rigidbody.AddForce(-hit3.normal * shotgunForce);
}
/* ------------------FIFTH SHOT---------------------*/
// Check if our raycast has hit anything
if (Physics.Raycast(rayOrigin4, out hit4, Mathf.Infinity))
{
// if the object hit has as target script attached, run the take damage function
Target target = hit4.transform.GetComponent<Target>();
if (target != null)
{
target.takeDamage(shotgunDamage);
}
GameObject impactEffectInstance = Instantiate(impactEffect, hit4.point, Quaternion.LookRotation(hit4.normal));
Destroy(impactEffectInstance, 1f);
}
// Check if the object we hit has a rigidbody attached
if (hit4.rigidbody != null)
{
// Add force to the rigidbody we hit, in the direction from which it was hit
hit4.rigidbody.AddForce(-hit4.normal * shotgunForce);
}
}
void PlayerFireAlternative()
{
Debug.Log("You fired your weapon's alternate fire");
}
}