What do you want to achieve? A raycast with a spread
What is the issue? the code I’ve written doesn’t seem to add a spread like how I was expecting it to
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)
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
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