Odd Behavior with Raycast Normals

Hello everyone! I’m currently doing some work with raycast normals, and I’ve come across this really weird behavior, as seen in this video here

That’s obviously just a demo of the issue, but I’m really stumped on what the root cause is. My code is below, if that helps (just a mockup for testing).

local function ray(pos)
	return workspace:Raycast(pos,Vector3.new(0,-100,0))
end

script.Parent:WaitForChild("Part").Changed:Connect(function()
	local r = ray(script.Parent.Part.Position)
	if r then
		script.Parent.Snap.CFrame = CFrame.new(r.Position,r.Position + r.Normal)
	end
end)

Any help is greatly appreciated, I’m genuinely confused as to what’s going on.

2 Likes

What do you mean by “Odd Behavior”?
Do you mean that it’s offset?

I think its the rotation shown in the video on the part moving below, it rotates 90 degrees left as it seems

I believe its because your using Position instead of CFrame (r.Position,r.Position

Yes, that is the issue I’m experiencing right now.

If you are using the CFrame.new() method you will need to take into account the previous right vector.

You can use cross product like below

	local rightVector = (bg.CFrame.lookVector:Cross(upVector)).Unit

Or use a different rotation method:

1 Like

Tysm! This helped me fix the problem!

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