How To Spray Random Raycasts In A Cone Shape

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    Raycast a cone shape which can have a variable width
  2. What is the issue? Include screenshots / videos if possible!
    I am unable to do so with my current understanding of raycasts
--angle can vary anywhere between 5 and 50
local RayDirection = Vector3.new(Random.new():NextNumber(-angle, angle), Random.new():NextNumber(-angle, angle), Random.new():NextNumber(-angle, angle))

Please do not ask people to write entire scripts or design entire systems for you. If you can’t answer the three questions above, you should probably pick a different category.

2 Likes

Weird, I just answered this question:

4 Likes

Im having issues implementing this, no rays seem to show up or hit anything at all,

Could you share the code you’re using to call it?

				local RayPosition = caster
				local beam = Tool.Pistol.Part.Beam
				local final = script.PointCount
				local beamea = Tool.Pistol.Part.BeamEndAttachment
				local angle = Player.PlayerGui.ScreenGui.Frame.angle.angle.Value
				local RayDirection = RandomCone(caster.CFrame.LookVector, math.rad(45))
				warn("RayDirection Set")
				local Params = RaycastParams.new()
				Params.CollisionGroup = "Default"
				Params.FilterType = Enum.RaycastFilterType.Blacklist
				Params.FilterDescendantsInstances = {RayPosition}

				local Result = workspace:Raycast(RayPosition.Position, RayDirection, Params)

(45 for testing)

RandomCone returns a vector of length 1. You have to multiply it by the length you want your ray to actually go.

1 Like

Ah, thank you. I didnt realize that

how would i make this do a spray of raycasts in the shape of a cone? im trying to achieve something like a field of view of a part with rays

You could call the random vector function over and over, but you may find this method more applicable to your use case:

That will distribute however many vectors you want evenly within a 3D cone.

1 Like