Bullet spread and visualization

  1. What do you want to achieve? bullet spread system

  2. What is the issue? -

  3. What solutions have you tried so far? I tried to find solutions on devforum

Hello, I want to make bullet spread system and bullet visualization with raycast, what is the best way for me to do this?

For now I use this :

		local Pos = currentGun.Fire.Position
		local hit = plr:GetMouse().Hit

		local origin = Pos
		local direction = (hit.Position-Pos)*1000

		local params = RaycastParams.new()
		params.FilterDescendantsInstances = {char, camera}
		params.FilterType = Enum.RaycastFilterType.Exclude

		local ray = workspace:Raycast(origin, direction, params)
1 Like

You can use math.random() to get random position:

local direction = (hit.Position-Pos)*1000
direction *= CFrame.Angles(math.radians(math.random(0,3)), math.radians(math.random(0,3)), math.radians(math.random(0,3)))

Tell me if it works

Error : ‘Attempt to call a nil value’

Please show me your edited code and where the error ocurred

		local Pos = currentGun.Fire.Position
		local hit = plr:GetMouse().Hit

		local origin = Pos
		local direction = (hit.Position-Pos)*1000
		direction *= CFrame.Angles(math.radians(math.random(0,3)), math.radians(math.random(0,3)), math.radians(math.random(0,3))) --here im getting error 'Attempt to call a nil value’
		local params = RaycastParams.new()
		params.FilterDescendantsInstances = {char, camera}
		params.FilterType = Enum.RaycastFilterType.Exclude

		local ray = workspace:Raycast(origin, direction, params)

Ah yes, my bad. Change math.radians to math.rad

Error - ‘invalid argument #1 (CFrame expected, got Vector3)’
Here is my edited code

local direction = (hit.Position-Pos)*1000
direction *= CFrame.Angles(math.rad(math.random(0,3)), math.rad(math.random(0,3)), math.rad(math.random(0,3)))

Then this should work:

local spreadAmount = 5 -- Adjust this value to increase or decrease spread
local randomSpread = Vector3.new(math.random(-spreadAmount, spreadAmount), math.random(-spreadAmount, spreadAmount), math.random(-spreadAmount, spreadAmount))
local direction = ((hit.Position + randomSpread) - Pos) * 1000
1 Like

Oh yeah, thats really working, but still 1 question, what is the best way for me to create bullets and visualize them?

Edited : And in your code i cant change spreadAmount value lower than 1, how can i fix this?

1 Like

I suggest trying out FastCast, it’s a far better alternative rather than default roblox raycasting, also offers ways to visualize bullets (Also mark my solution pls):

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.