RayCast not working Properly

I want to be able to detect if a wall is in front of a player or not. The problem that I’m running into is that the RayCast function is returning true, even if there isn’t a wall in front of the player. I used constraints to visualize the ray but that was working normally so I don’t really know where I went wrong. I looked for solutions on the DevHub and also here, on DevForum, but they didn’t have the same problem as me.

Here’s the code:

function isWall() 
	
	local rayOrigin = HumanoidRootPart.Position;
	local rayDirection = HumanoidRootPart.CFrame.LookVector * Vector3.new(3,3,3) + HumanoidRootPart.CFrame.Position;
	
	local rayParam = RaycastParams.new();
	rayParam.FilterType = Enum.RaycastFilterType.Blacklist;
	rayParam.FilterDescendantsInstances = wallParam --wallParam just consists of the Character's children.
	
	local rayCast = workspace:Raycast(rayOrigin,rayDirection,rayParam);
	
	visualizeRay(rayOrigin,rayDirection) --Just a function to visualize the ray via constraints
	
	if rayCast then
		return {true,rayCast.Normal}
	else
		return false
	end
	
end

Now if I run this function when I stand over here,


It returns false. Notice how the rod is touching the wall, but it still returns false.

If I run this function when I stand over here,
image
It returns true, even if the ray didn’t go in that direction. It could just be that my rod constraint script isn’t properly visualizing the ray so I’ll leave the script for that here just in case.

function visualizeRay(origin,direction)
	local AttachmentA = Instance.new("Attachment",workspace.Terrain)
	local AttachmentB = Instance.new("Attachment",workspace.Terrain)
	
	AttachmentA.Position = origin
	AttachmentB.Position = direction
	
	local RodConstraint = Instance.new("RodConstraint",workspace.Terrain)
	RodConstraint.Attachment0 = AttachmentA
	RodConstraint.Attachment1 = AttachmentB
	
	RodConstraint.Visible = true
end

other than that, I’m pretty much done. I hope you guys can find a solution. Thank you.

2 Likes

Couldn’t you can’t just reference it like this?

	local rayDirection =  (rayOrigin + HumanoidRootPart.CFrame.LookVector) * 3;

That didn’t really cross my mind, but yeah it would have worked.

Also sorry for putting so many semi colons in the script inconsistently. I’m learning a new language so I’m trying to get a good habit of putting semi colons after a line.

It looks like the problem was with the parameters. I used GetChildren() instead of GetDescendants() for wallParam so that’s why it wasn’t working.