How would I make a Raycast shotgun?

Before anyone tells me to, I have already searched for it online, and I also saw the post in this forum. But the problem is, they give out a uniform result, which is not what I want. I want it to be actually random like an actual shotgun. Any tips?

1 Like

try this maybe it should help:

hope it does

2 Likes

Thanks for the help, appreciate it, but unfortunately this is not what I was looking for. Though I know how to do raycasting, I don’t know how I could make the shotgun’s raycast spread, or even clone it multiple times at once

This has been asked and answered plenty of times before, but here is my solution to a post that had the exact same question (which can be found here.)

Essentially, you’d create a loop that would create multiple raycasts, each with varying directions (you can add randomized directions on it by using Vector3.new()).
Here’s the code if you’re interested.

local pelletCount = 7 --The number of pellets you want being shot
local maximumOffset = 1 --The maximum number of studs that the bullets will offset at
local RNG = Random.new()

for i = 1, pelletCount do --Create a loop that repeats itself based on the amount of pellets being shot
   local pelletSpread = Vector3.new(RNG:NextNumber(-maximumOffset, maximumOffset), RNG:NextNumber(-maximumOffset, maximumOffset), RNG:NextNumber(-maximumOffset, maximumOffset)) --Create a Vector3 that consists of a randomization of all the axes.
   local ray = Ray.new(tool.Object.FirePart.Position,(MouseHit.LookVector + pelletSpread) * 300) --Create an equation of Mouse.Hit.LookVector + the pelletSpread.  
end
3 Likes

MouseHit.LookVector is direction from the Camera to the mouse, so the Ray fired will be at the wrong spot. The real direction should be from the front of the gun to the mouse hit point.
In this case it should be: (MouseHit.Position-tool.Object.FirePart.Position).Unit.