Why the hell is my raycastResult only detecting certain faces of a part

I’m extremely frustrated right now, because I don’t know how to fix this at all.

I’m trying to make a wall climbing mechanic by using raycast, but for some reason, my raycast result is only detecting certain faces of a part.

This is really annoying, and I literally at all do not know how I’m supposed to fix this. I really need someone to help. The raycast visual part isn’t moving at all either.

There’s no errors, so please don’t ask that.

local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()

local origion = character.PrimaryPart
local direction = origion.CFrame * CFrame.new(0, 0, -2)
local params = RaycastParams.new()

local rayPart = Instance.new("Part")
rayPart.Name = character.Name.." Ray Part"
rayPart.Anchored = true
rayPart.CFrame = direction
rayPart.Material = Enum.Material.SmoothPlastic
rayPart.CanCollide = false
rayPart.Parent = workspace.Terrain
rayPart.Size = Vector3.new(1, 1, 2)

params.FilterDescendantsInstances = {character, rayPart}
params.FilterType = Enum.RaycastFilterType.Blacklist
params.IgnoreWater = true

while task.wait() do
	rayPart.CFrame = direction
	
	local raycastResult = workspace:Raycast(origion.Position, direction.Position - origion.Position, params)
	
	if raycastResult then
		print(raycastResult.Instance)
	else
		warn("ekjexkkeinnxe")
	end
end

Well from the code it looks like you are firing a ray from the players head back to the origin rayPart so I would think you would only see the faces that are toward the player as once you go past those parts they are not between the player and the rayPart.

Usually there are issues when doing raycasts using the exact length to an object. I assume this is due to floating point errors. What I usually do instead is send the ray a set length. To do this from your current code you’d do:

ray = (direction.Position - origion.Position).Magnitude * length -- Replace length with how many studs

*Side note, if you mean “origin” it is spelt “origin” and not “origion”

Also, as @quakage pointed out you have an issue with direction. Your direction is calculated once for what the position was when the code was run the first time.

Lets say the Character.PrimaryPart is at Vector3.new(0, 0, 0) for simplicity’s sake. That means that direction is Vector3.new(0, 0, -2). Every loop direction stays that way, which means it does not account for any movement. Instead you want to move the direction calculation into that while loop.

1 Like

I’m trying to fire a ray from the players root part forward by 5 studs.

If that is the case then like @ifkpop said you need to copy your line to set the direction down into you while loop right before you do the Raycast and just use direction.Position as the target. You don’t need the rayPart either but maybe you just created that to see where that direction was.

1 Like

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