Strange raycast behavior

So I am working on a system that should send a raycast from the HRPs position out to the left of the player, now from the look of things, it works, but something strange happens.

So here is my demonstration on what happens;
If I come from the front of the box and the raycast fires, yippie it knows the part is there!
Same with the right side of the object, but when I come from the left or back, it just doesnt trigger.
I’ve checked my params for the raycast, I’ve gone out of the way to display where the raycast is firing too (which it is always in the right place) alas, still, when I come up to that side of the part, it fails.

I have the spawn surrounded by some parts, to demonstrate which sides the raycast is triggering on for them observe ( The sides with green are the sides the raycast does trigger on)


As we can see there is a pattern here, the problem is now what can I do about it?
It seems to be all based on where the position (0,0,0) is, cause if I we’re to move the parts away from that area, the results would change. Overall this is just the most confusing thing I’ve ever had to deal with. Heres my code:


Main Code

		OnLeftWall = function(Ignore)
			local isTouching,Objects = isTouching()
			
			
			if isTouching  then
				local params = getParams(true,Ignore)
				local originPo = root.CFrame.Position
				local targetCframe = root.CFrame * CFrame.new(-1,0,0)
				local targetPo = targetCframe.Position
				local po2 = getPO2(CFrame.new(raycastOffsets.Left,0,0),char)
				
				local debugPart:BasePart = createDebugPart(po2,Vector3.new(1,1,1),nil,true)
				if debugPart then
					debugPart.Color = Color3.new(1, 0.666667, 0)
				end
				
				local raycast = workspace:Raycast(originPo,po2,params)
				
				if raycast then
					local hit:BasePart = raycast.Instance
					
					print(hit)
				end
			end
		end,

GetParams

PS: I am 100% that the raycast params are not ignoring the parts, I’ve checked over whats being added, and the parts are not getting ignored.

local function getParams(RP,Ignore)
	local params = RaycastParams.new()
	
	if not RP then
		params = OverlapParams.new()
	end
	
	local ignoring = {}

	-- || Handle Items to Ignore
	if Ignore  then
		for _, item in Ignore do
			table.insert(ignoring,item)
			if item:IsA("Instance")  then
				for _, d in item:GetDescendants() do
					table.insert(ignoring,d)
				end
			end
		end
	end

	-- || Ignore Characters ||
	for _, char in workspace:GetDescendants() do
		if char:IsA('Model') then
			if char:FindFirstChildOfClass('Humanoid') then
				table.insert(ignoring,char)
				for _, item in char:GetDescendants() do
					table.insert(ignoring,item)
				end
			end
		end
	end

	params.FilterType = Enum.RaycastFilterType.Exclude
	params.FilterDescendantsInstances = ignoring
	
	return params
end

Get Po2

function getPO2(distance,char)

	local part = Instance.new('Part') -- the reason we make a part is cause other wise it will act weird

	local po = char.PrimaryPart.CFrame * distance

	part.CFrame = po


	local po2 = part.Position

	part:Destroy()

	return po2
end

If you have any questions about anything please do ask me!

1 Like

Simple Left Raycast.rbxl (53.2 KB)
I made a simple left raycast script, it will print the part name that is to the left of the player’s humanoidrootpart

-- LocalScript inside StarterCharacterScripts
local char = script.Parent

local hrp = char:WaitForChild("HumanoidRootPart")

local leftRayDist = 10

local rayParams = RaycastParams.new()
rayParams.FilterDescendantsInstances = {char}

game:GetService("RunService").RenderStepped:Connect(function()
	local rayResult = workspace:Raycast(hrp.Position, -hrp.CFrame.RightVector * leftRayDist, rayParams)
	if rayResult and rayResult.Instance then
		print("left: ", rayResult.Instance)
	end
end)

If you want the left direction vector of a part (such as HumanoidRootPart), just use -part.CFrame.RightVector

Well this does work, but it doesnt explain why this is happening.

You don’t need to loop over the character’s inner parts, putting the character model in will already filter out the child parts automatically.

This code is unused, so I might ignore it.

I don’t think there’s a difference between root.Position and root.CFrame.Position

Really? You made the direction vector to be char.PrimaryPart.CFrame * distance, you’re suppose to give an offset from the origin position:
workspace:Raycast(originPosition, direction, raycastParams).

The reason that it seem like to base off origin (0,0,0) because you didn’t give a direction, you gave a position. And this is where raycast will be raycasting at, from originPosition to: originPosition + direction

This is what your code is doing:


Since raycast will be raycasting from originPosition to: originPosition + direction
And just assuming your RaycastOffset.Left = -10.

Just stick to using -part.CFrame.RightVector to get the Left Direction Vector of a part, and multiply it by the number that you want how long the ray should go.
-part.CFrame.RightVector * 100

You don’t need to deal with CFrames. Raycast uses Vector3 anyway.

Buttt, if your intention is to find between two positions then:

local originPo
local po2

workspace:Raycast(originPo, (po2 - originPo), raycastParams)

By doing po2 - originPo, you get the direction vector from originPo to po2

Yeah, while I was testing it, the raycast kept getting triggered on the parts of the body even when I put the just the char in there so I added that, not realizing I just didnt use the params, and well as you can tell I forgot to remove it, oops lol.

Yeah you’re right on that one, why it ended up that way was me fiddling with the code trying to figure it out.

I made this a year ago ok. . . I was skilled back then :<

Overall yes this does make a lot of sense so hey, thanks! This cleared up my problems,
tysm~
~ Frodev

No problem, glad to help! It’s normal to make mistakes. I used to make a lot of dumb mistakes too…

1 Like

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