Is there any way to make a shot gun spread using mouse.Target and mouse.Hit.p?

I have a game thats in first person, the gun manager uses mouse.Target as a way to get who youre shooting, since my game is first person, it doesnt matter that it uses mouse.Target because since its in first person you will not be able to zoom out and hit people around walls.

Anyway i want to know if you can make a shot gun spread using mouse.Target, i want the shot gun to have a wider range using mouse.Target and mouse.Hit.p. Is it possible to make the mouse target box a little bigger?

2 Likes

Just add a random position to it?

mouse.Hit.Position + Vector3.new(math.random()-0.5,math.random()-0.5,math.random()-0.5) * 10

Where 10 is the intensity of the spread. It’s also recommended to not use the above method as is because it would be unrealistic because the shotgun will have insane spread at insanely close distances. Rather, you can use Camera:ScreenPointToRay() and UserInputService:GetMouseLocation() to do something like:

local UserInputService = game:GetService("UserInputService")
local length = 500
local spread = 10

local mouseLocation = UserInputService:GetMouseLocation()
local unitRay = camera:ScreenPointToRay(mouseLocation.X, mouseLocation.Y)

local destination = unitRay.Origin + unitRay.Direction * length
destination += Vector3.new(math.random()-0.5,math.random()-0.5,math.random()-0.5) * spread

local extendedRay = workspace:RayCast(unitRay.Origin, destination - unitRay.Origin)
2 Likes

and we get what the ray hits to be the player we are attacking?

1 Like

Randomize the directional vector a lil bit, it would be fine.

1 Like

i dont understand what you mean?

1 Like

Uhhh… yes we get the player who we shot? Add a raycast param if you’re getting the shooter.

1 Like

Every raycast has a directional vector in it, add some randomness to its X, Y, and Z values.

1 Like

i am not familiar with raycasts that much.

1 Like
	local UserInputService = game:GetService("UserInputService")
	local length = 500
	local spread = 10

	local mouseLocation = game.UserInputService:GetMouseLocation()
	local unitRay = camera:ScreenPointToRay(mouseLocation.X, mouseLocation.Y)

	local destination = unitRay.Origin + unitRay.Direction * length
	destination += Vector3.new(math.random()-0.5,math.random()-0.5,math.random()-0.5) * spread
	
	local raycastParams = RaycastParams.new()
	raycastParams.FilterDescendantsInstances = {script.Parent}
	raycastParams.FilterType = Enum.RaycastFilterType.Exclude
	raycastParams.IgnoreWater = true

	local extendedRay = workspace:Raycast(unitRay.Origin, destination - unitRay.Origin, raycastParams)
	print(extendedRay.Instance.Name)
	
	
	game:GetService("ReplicatedStorage").REMOTES.shotgun:FireServer(tool,mouse.Target,mouse.Hit)	

i need it to be able to get multiple humanoids

1 Like

For that, just use a for index = 1,[amountofbullets] do and use a table to store the raycasts and just calculate everything needed accordingly.

1 Like

btw the length and spread variables do nothing

1 Like