How can I make the Raycast give me the top surface of an object

Hello! I’ve been trying for some time to get the raycast to make a Part in front of the RayCastResult.Position but I have a problem because first I try to change the position of the object with vector3 so that it does not collide with the Part but I get stuck if the other part has a different size becasuse it collides.

How can i get the front of the RayCast.Position so that the part depending on the size it has, always looks good and does not collide.

Any help is appreciated :smile:

Use the ray’s normal vector to offset the part based on the length of the block on the corresponding axis
You will also need to orient the part to be parallel to the normal vector for this to work

Here’s an example:

--put this in a localscript; click on a surface to spawn a part on it
local param = RaycastParams.new()
local lp = game:GetService('Players').LocalPlayer
local m = lp:GetMouse()

m.Button1Down:Connect(function()
	local origin = workspace.CurrentCamera.CFrame.Position 
	local ray = workspace:Raycast(
		origin,
		(m.Hit.Position - origin) * 2,
		param
	)
	if ray then
		local part = Instance.new('Part')
		part.Anchored = true
		part.Color = Color3.new(1)
		
		part.CFrame = CFrame.lookAt(ray.Position, ray.Position + ray.Normal) * CFrame.new(0, 0, -part.Size.Z * .5)
		
		part.Parent = workspace
	end
end)

3 Likes