Rotating a part to have face point the same direction as another part along one axis

I have two parts: a “doorway” and a “plank”. The user can create planks on a doorway where a random point on the left of the doorway maps to a random point on the right, and a plank is created to bridge the gap. This works well when the doorway is at a 90 degree angle from the ground, but once it’s rotated diagonally, the planks don’t rotate so they’re facing outwards the same way as the doorway and it looks messy (I want there to be Z-fighting so I know they’re on the same plane).

The left one is rotated 45 degrees, the middle one is perpendicular to the ground, and the right one is what I want the left one to look like (rotated in studio).

Manually rotating the planks in studio show that planks going straight left-right need to be rotated the same amount as the doorway, but all the diagonal planks need some funky amount between the rotation of the doorway and 0 (0 is when a plank is straight top-down).

Some math could probably solve this, but I’m wondering if there’s a simpler solution to get the side of the plank to face the same direction as the doorway for all planks.

Code:

    local pointsL = {}
	local pointsR = {}
	local side = (char.HumanoidRootPart.Position - script.Parent.sideDetector1.Value.Position).Magnitude > (char.HumanoidRootPart.Position - script.Parent.sideDetector2.Value.Position).Magnitude and -1 or 1
	for i = script.Parent.Position.Y + (script.Parent.Size.Y / 2), script.Parent.Position.Y - (script.Parent.Size.Y / 2), -2  do
		table.insert(pointsL, script.Parent.CFrame:ToWorldSpace(CFrame.new(-(script.Parent.Size.X / 2), i - script.Parent.Position.Y, side * (script.Parent.Size.Z / 2))))
		table.insert(pointsR, script.Parent.CFrame:ToWorldSpace(CFrame.new((script.Parent.Size.X / 2), i - script.Parent.Position.Y, side * (script.Parent.Size.Z / 2))))
	end
	while #pointsL > 0 do
		wait(1)
		local part = Instance.new("Part")
		local point1 = table.remove(pointsL, math.random(#pointsL))
		local point2 = table.remove(pointsR, math.random(#pointsR))
		part.CFrame = CFrame.lookAt(point1:Lerp(point2, 0.5).Position, point2.Position)
		part.Size = Vector3.new(1, 1, (point1.Position - point2.Position).Magnitude + 1)
		part.Anchored = true
		part.Parent = game.Workspace
		part.BrickColor = BrickColor.new("Medium brown")
		part.Material = Enum.Material.Wood		
	end

Solved: I was missing the UpVector in CFrame.lookAt(). Setting the UpVector parameter to the UpVector of the doorway oriented it along the doorway’s axis

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