Why is this raycast not working?

So I made a raycast for a weather system to detect if there’s shelter(by detecting if there’s something in the way) but since the part I used is too big and detects other stuff, I used a smaller part.

But for some unknown reason, that smaller part has the same issue?

Its detecting stuff from miles away even though its only meant to raycast to my head.

Yes it’s a really messy script

local rs = game:GetService("ReplicatedStorage")
local runservice = game:GetService("RunService")

local particle = rs:WaitForChild("Particle")

local clone = particle:Clone()
local clone2 = rs:WaitForChild("Origin"):Clone()

clone2.Parent = workspace
clone.Parent = workspace
local char = script.Parent
local head = char:WaitForChild("Head")

clone.Position = head.Position

local function isObstructed(PointA_Position, PointB_Position)
	local Direction = (PointB_Position - PointA_Position) -- gives us our direction towards PointB_Position
	local Raycast = Ray.new(PointA_Position, PointB_Position) -- Multiplier, just in case.
	local Hit = workspace:FindPartOnRay(Raycast) 
	--workspace:FindPartOnRay() also has three more parameters you can pass through, respectively: what the Ray should ignore, whether the Terrain cells are cubes, and whether the ray should ignore the Enum.Material.Water.

	if Hit then
		return Hit -- If there was anything that got hit by the Raycast variable, return true.
	else
		return false -- If there wasn't anything in the way, return false.
	end
end

runservice.RenderStepped:Connect(function()
	clone2.Position =  head.Position + Vector3.new(0,clone.Size.Y * 5,0)
	clone.Position = head.Position + Vector3.new(0,clone.Size.Y * 5,0)
	clone.ParticleEmitter.Texture = rs.Texture.Value
	clone.ParticleEmitter.Speed = NumberRange.new(rs.Speed.Value)
	if isObstructed(clone2.Position, head.Position) then
		print(isObstructed(clone2.Position, head.Position))
		clone.ParticleEmitter.Enabled = false
	else
		clone.ParticleEmitter.Enabled = true
	end
end)
4 Likes

Hey there!

When creating a new Ray, you need to pass 2 Vector3 properties. One of them is the origin of the ray, and the other one is the direction which isn’t relative to the WorldSpace. Apparently, instead of the Direction, you’re trying to pass PointB_Position which isn’t relative to the ray origin, so it doesn’t give an appropriate result.

The workaround is quite easy! Just change PointB_Position to Direction in the script when creating a new Ray:

local Direction = (PointB_Position - PointA_Position) -- gives us our direction towards PointB_Position
local Raycast = Ray.new(PointA_Position, Direction) -- Multiplier, just in case.
local Hit = workspace:FindPartOnRay(Raycast) 

I think perhaps your ray is pointing down just based on the prints. Just simply setting the direction to up, Vector3.new(0, 1, 0) * 50 will do what you want. Make the origin the head, exclude the player’s character from the ray, and set 50 to how far you want the ray to reach.

Heres a basic example I made which does this and prints successfully


task.wait(3)

local params = RaycastParams.new()
params.FilterType = Enum.RaycastFilterType.Exclude
params.FilterDescendantsInstances = {game.Workspace.Rynappel}	

while true do
	local head = game.Workspace["Rynappel"].Head :: BasePart
	local ray = workspace:Raycast(head.Position, Vector3.new(0, 1, 0) * 50, params)
	
	print(ray)
	task.wait(1)
end