What's wrong with my raycast function?

Hi guys, I wrote a simple function that fires a ray given a start CFrame and an end CFrame. The filter is set to exclude all the baseparts in the living player folder in the workspace. For some reason though, the code keeps printing no result, and returning the EndPos argument no matter what, even if I am certain the ray is going straight through a wall. Does anybody know what’s wrong here?

function QuickCast(StartPos, EndPos, Length)
	local Filter = RaycastParams.new()
	Filter.FilterType = Enum.RaycastFilterType.Exclude
	for _, V in pairs(workspace.Alive:GetDescendants()) do
		if V:IsA("BasePart") then
			Filter:AddToFilter(V)
		end
	end
	
	local SP = (StartPos.Position - EndPos.Position).Unit * Length
	local Result = workspace:Raycast(StartPos.Position, SP, Filter)
	local Ending	
	if Result then
		Ending = CFrame.new(Result.Position)
	else
		print("No result")
		Ending = EndPos
	end
	return Ending
end
1 Like

It is EndPos - StartPos, a negative also solves it:

local SP = (EndPos.Position - StartPos.Position).Unit * Length --> this
local SP = (-StartPos.Position + EndPos.Position).Unit * Length --> or this
local SP = -(StartPos.Position - EndPos.Position).Unit * Length --> or this

[spoiler] I didn’t know they added AddToFilter LOL[/spoiler]

1 Like

Of course I overlooked the most obvious thing and was scratching my head for 30 minutes :melting_face:
Thank you!!

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.