How to make a part face the same way as a specific side of a wall

I’ve been trying for quite a while now to figure out how to make a part face the same side as a wall that a raycast hit. For some reason raycastresult.Normal doesn’t give the proper rotation of the wall.

I want the parts to look like this on different sides of the wall
image

Instead I get this
image

It seems nobody has had this problem before, because I tried to look up solutions but I got nothing related to this

Here’s the code for rotating the part:

	mine.Orientation = char.HumanoidRootPart.Orientation
	local ray3 = Ray.new((mine.CFrame * CFrame.new(0,0,1)).p,mine.CFrame.LookVector * 4)
	local hit,pos = workspace:FindPartOnRayWithIgnoreList(ray3,{mine})
	
	if hit then
		local params = RaycastParams.new()
		params.FilterType = Enum.RaycastFilterType.Blacklist
		params.FilterDescendantsInstances = {mine}
		local rrray = workspace:Raycast(ray3.Origin,ray3.Direction,params)
		
--- "grounded" is just a variable that tells the script if the part landed on the ground or not
		if grounded == false and rrray and rrray.Instance ~= mine and rrray.Instance.CanCollide == true and rrray.Instance.Parent:FindFirstChild("HumanoidRootPart") == nil then
			mine.Orientation = Vector3.new(0,rrray.Normal.Y,0)
			mine.CFrame = mine.CFrame * CFrame.Angles(math.rad(rrray.Normal.X),math.rad(rrray.Normal.Y),math.rad(rrray.Normal.Z))
			
end
end

“mine” is the part I want to rotate

I know it’s possible to do this because another game did it

Use the advanced CFrame technique to rotate the parts UpVector to align/ face the sameway with the walls normal vector.

The function we will be needing

local function getRotationBetween(u, v, axis)
    local dot, uxv = u:Dot(v), u:Cross(v)
    if (dot < -0.99999) then return CFrame.fromAxisAngle(axis, math.pi) end
    return CFrame.new(0, 0, 0, uxv.x, uxv.y, uxv.z, 1 + dot)
end

Using it:

		if grounded == false and rrray and rrray.Instance ~= mine and rrray.Instance.CanCollide == true and rrray.Instance.Parent:FindFirstChild("HumanoidRootPart") == nil then
local rotateToSurfaceCF = getRotationBetween(mine.CFrame.UpVector,rrray.Normal,Vector3.new(0,1,0))
mine.CFrame = mine.CFrame * rotateToSurfaceCF
end
3 Likes