How can I offset this raycast in randomly?

local raycastResult = workspace:Raycast(script.Parent.Handle.Position,(mousePos - script.Parent.Handle.Position)*70,raycastParams)

That is my raycast, I have tried putting a *(number) after script.Parent.Handle.Position, but It becomes really buggy.

Elaborate :face_with_raised_eyebrow:


sorry for my trash drawing skills

*math.random(-2,2) / math.random()

sometimes it does not i dont see the raycast is should i make any changes to the beam script?

	local distance = (script.Parent.Muzzle.Position - raycastResult.Position).Magnitude
		local p = Instance.new("Part")
		p.Parent = game.Workspace
		p.Name = ("Gun Trail")
		p.Anchored = true
		p.CanCollide = false
		p.Transparency = 0.5
		p.Size = Vector3.new(0.1, 0.1, distance)
		p.Material = Enum.Material.Neon
		p.Color = Color3.new(1, 0.701961, 0)
		p.CFrame = CFrame.lookAt(script.Parent.Muzzle.Position, raycastResult.Position)*CFrame.new(0, 0, -distance/2)
1 Like

Why are you making it .lookAt? Very random. Also .lookAt returns CFrame Orientation, this wouldn’t make it move.

it doesnt move it just visualizes
a part

So it rotates towards a part? Or maybe I didn’t understand

This is stolen borrowed from the fastcast module.

-- Local
local FireDirection = (Mouse.Hit.p - FirePointObject.WorldPosition).Unit  -- Firepoint object is an attachemnt on the gun

-- Server
local RNG = Random.new() 

-- Inside a fire function which is called whenever player fires
local directionalCF = CFrame.new(Vector3.new(), FiringDirection)
local direction = (directionalCF * CFrame.fromOrientation(0, 0, RNG:NextNumber(0, TAU)) * CFrame.fromOrientation(math.rad(RNG:NextNumber(MIN_BULLET_SPREAD_ANGLE, MAX_BULLET_SPREAD_ANGLE)), 0, 0)).LookVector  -- Direction to cast ray

The important bit here is MIN_BULLET_SPREAD_ANGLE and MAX_BULLET_SPREAD_ANGLE, quoting from the fastcast module:
“THIS VALUE IS VERY SENSITIVE. Try to keep changes to it small. The least accurate the bullet can be. This angle value is in degrees. A value of 0 means straight forward.”

Although It’s better off if you directly refer to the module then ask me how it works, my eye’s usually glaze over when looking at this kind of maths.

1 Like