Raycasting Spread not working

  1. What do you want to achieve? A raycast with a spread

  2. What is the issue? the code I’ve written doesn’t seem to add a spread like how I was expecting it to

  3. What solutions have you tried so far? I’ve looked at a lot of posts and tried to use their solutions (but modified to fit my script) yet none of them have worked

here’s the raycast script i have

local CanaryRay = Ray.new(Character.Head.Position, (Character.Head.CFrame * CFrame.new(math.random(-4,4),math.random(-4,4),0)).LookVector * 20)

all help is appreciated

it helps if you could give more details. how do you expect the code to function, and how does it work at the moment? a picture or a video would help too.

even through i don’t know much of what you’re intending to do, i happen to have a spread function from another post, so perhaps you could try it:

--courtesy of: https://devforum.roblox.com/t/ideal-way-to-make-shotgun-spread/126567/7
function Vector_coneSpread(direction, maxSpread)
	local offsetRotation = CFrame.Angles(0,0,math.pi * 2 * math.random()) * CFrame.Angles(math.random() * math.rad(maxSpread),0,0)
	local offsetDir = (CFrame.new(Vector3.new(), direction) * offsetRotation).LookVector
	return offsetDir
end

using the function (i’m more used to worldRoot:Raycast() instead of Ray.new() so i might not get it right):

local CanaryRay = Ray.new(Character.Head.Position, Vector_coneSpread(directionVector, maxSpread))

Yeah I probably should have, its a raycast that fires from the head of the character to about like 10-20 studs away and it stays contunious wether they turn or not. It’s for a sonic screaming ability and having it face just one direction instead of being spread out didn’t really work the way I wanted it to. Im gonna try your code and see if it works

According to this post Ray.new() is deprecated and you should use workspace:Raycast()

apparently tsering mentioned that Ray.new() is deprecated so here’s a version using worldRoot/workspace:Raycast():

local directionVector = (Target.HumanoidRootPart.Position - Character.Head.Position).Unit
local raycast = workspace:Raycast(Character.Head.Position, Vector_coneSpread(directionVector, maxSpread) * Range)

if raycast and raycast.Instance:IsDescendantOf(Target) then --check if the instance hit belongs to the target
	local Humanoid = Target:FindFirstChildOfClass("Humanoid")
	if Humanoid then
		Humanoid:TakeDamage()
	end
end