Raycasting issues

Hello there.

I’m currently working on this little system of mine, when I noticed that the raycasts are producing inaccurate results.

After looking into it I narrowed down the cause of it to the direction the raycast is firing in. After creating parts to see where the raycast is being fired too, it turns out the direction is based on where the player originally was?

For context I am trying to fire a raycast about 50 studs out in front of the player, I tested this by spinning in a circle, and it works fine when I do it without walking around at all, but the second I start moving away from where I spawned I started noticing something wasnt right.

Example:

After spinning in a circle whilst the raycats were firing, I can see that they were all fired around spawn?? The blue blocks is where it fired too, and the red blocks are where it fired from, aka where I am.

I have no idea what the issue is, so I’ve come here!

Here is some of my code!


Raycast code

function Class:RaycastInDirection(position:Vector3, direction:Vector3, Filter:HitboxFilter, ActionName:string) : (BasePart, Vector3)
	
	-- // Params
	local Params:RaycastParams

	if ActionName and Class._Params[ActionName] then
		Params = Class._Params[ActionName]
	else
		Params = RaycastParams.new()
	end

	Params.FilterDescendantsInstances = OOPLibrary:CombineTables(Filter:GetFilterInstances(), GetIgnore())
	
	-- // Raycast
	local Result = workspace:Raycast(position, direction, Params)
	
	if not Result or not Result.Instance then
		-- // Debug
		Class:NewDebugBox(CFrame.new(position))
		Class:NewDebugBox(CFrame.new(direction), nil, Color3.fromRGB(0,1,0))
		print('Make Part:', direction)
	end
	
	if not Result or not Result.Instance then return nil end
	
	-- // Get Target
	local Target = Result.Instance
	
	if Target:FindFirstAncestorOfClass('Model') then
		Target = Target:FindFirstAncestorOfClass('Model').PrimaryPart or Target
	end
	
	-- // Finish Up
	return Target, Result.Position
end

Code that triggers the function

		local Target:BasePart, TargetPosition = HitboxModule:RaycastInDirection(
			Root.CFrame.Position, 
			Root.CFrame.LookVector * 50, 
			CharacterFilter
		)

PS Root is the HumanoidRootPart of the character.
Any idea whats going on? Thanks in advance!

I’ve personally seen this pattern of problem where if the raycast doesn’t hit anything then the destination/visualization parts become located at position 0, 0, 0 + the direction. I think the fix is to move the part to the origin position of the raycast + the direction. I don’t know exactly what you would change in your code but here is a post of a similar problem caused by not hitting something with a raycast

A note about the code, you can just return at the end of the if not Result or not Result.Instance block.

1 Like

Well this seems to work, but now that I can tell it is firing to the right point, I have another issue :sob:

For whatever reason, sometimes the raycast goes straight through the object, Im pretty sure it only happens when you’re like touching the object. Its still using all of this code, but Im not sure why its doing this :sob:

I take it this has to be related to how robloxs raycast system works, but I dont know any workarounds for it, so any ideas?

Have you tried printing Root.CFrame.Position every time you call the function? Is it correctly updating to where your character is?

I dont exactly print it, but all the red blocks appear where I am, and they are set ti be there, so I figure its correct!

Maybe i’m misinterpreting something here, but it seems like you’re just making the debug part at the direction? If so then this is the expected behavior. If you wanted the debug part to appear at the end of the raycast then you should put it at position + direction.

There’s 3 things that can cause this.

  1. The object in question has CanQuery disabled.
  2. The Origin is inside of the part.
  3. The RaycastParameters excludes said part from being detectable by the ray.

Quite often its case 2 and besides running some stupid check or moving the origin backwards, I’m not sure what else can fix it.

2 Likes

Oh yeah its probably 2 then :sob:

I guess I could probably just do workspace:GetPartsBoundInBox when it fails, lets try!

1 Like

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