I’ve been working on implementing a shotgun spread into a game using raycasts. The issue is that the subsequent raycasts only “hit” the object when the first raycast also “hits” that object. So, if only the lower pellet of the shot hits the target, it will not deal any damage. However if the the middle pellet also hits it along with the lower pellet it will deal the damage of both pellets as intended.
Here’s the relevant code for it:
public GameObject maincam;
public int damage = 10;
public float spreadT;
private BasicCombat combatinst1;
private BasicCombat combatinst2;
private BasicCombat combatinst3;
RaycastHit hit1;
RaycastHit hit2;
RaycastHit hit3;
void FixedUpdate()
{
ShotgunAttack(damage, spreadT);
}
void ShotgunAttack(float damageT, float spread)
{
if (Input.GetMouseButtonDown(0))
{
//original
if (Physics.Raycast(maincam.transform.position, maincam.transform.forward, out hit1, 100) == true)
{
combatinst1 = hit1.collider.gameObject.GetComponent<BasicCombat>();
combatinst1.health -= damageT;
}
//spread below
if (Physics.Raycast(maincam.transform.position, maincam.transform.forward + (-maincam.transform.up * spread), out hit2, 100) == true)
{
combatinst2 = hit2.collider.gameObject.GetComponent<BasicCombat>();
combatinst2.health -= damageT;
}
//spread above
if (Physics.Raycast(maincam.transform.position, maincam.transform.forward + (maincam.transform.up * spread), out hit3, 100) == true)
{
combatinst3 = hit3.collider.gameObject.GetComponent<BasicCombat>();
combatinst3.health -= damageT;
print(hit3.collider.gameObject);
}
}
}
Got any ideas?