Make player tilt towards a part

I want the player to tilt towards a part, it functions like a CFrame.LookAt but instead of setting the front surface of the humanoid root part to face the part, it uses the top, thus create a little tilt effect.
Heres my take on this: https://gyazo.com/2a181d1e08f0930b1e36d785ad3fbc63
It only applies for the Z axis, which is fine for me. However, the main issue here is when I move the part to the right, the player face the opposite direction of the part. And when I implemented the code into my main game its very messy and unusable
Heres the deplorable and simple code that I came up with

game.Players.PlayerAdded:Connect(function(plr)
	local hrp = plr.CharacterAdded:Wait().HumanoidRootPart
	hrp.Anchored = true
	
	local part2 = workspace.part2
	local up = hrp.CFrame.UpVector
	
	while wait() do
		local dist = (part2.Position - hrp.Position).Unit
		local angleZ = math.deg(math.acos((dist:Dot(up))))

		hrp.CFrame = CFrame.new(hrp.CFrame.p) * CFrame.Angles(0, 0, math.rad(angleZ))
	end
end)

I guess I would have to figure this out by myself

Well, if you just want the top surface of the part you can use CFrame.lookAt but rotate it 90 degrees forward on the x axis, like this:

game.Players.PlayerAdded:Connect(function(plr)
	local hrp = plr.CharacterAdded:Wait().HumanoidRootPart
	hrp.Anchored = true
	
	local part2 = workspace.part2
	
	while wait() do
		hrp.CFrame = CFrame.lookAt(hrp.Position, part2.Position) * CFrame.Angles(math.rad(90), 0, 0)
	end
end)
2 Likes

Thank you very much, it worked beautifully, I guess I kinda overthought myself.
By the way, I wonder if its possible to not make the player turn like this https://gyazo.com/d83c648f4fd280d5c8417f0518a9d3c4
Its a built-in function in roblox so I don’t think it’s possible but it’s worth a shot.

I’m not sure if that’s avoidable, but you can make it look nicer by lerping the cframe instead. Maybe try this?

game.Players.PlayerAdded:Connect(function(plr)
	local hrp = plr.CharacterAdded:Wait().HumanoidRootPart
	hrp.Anchored = true
	
	local part2 = workspace.part2
	
	while wait() do
		hrp.CFrame = hrp.CFrame:Lerp(CFrame.lookAt(hrp.Position, part2.Position) * CFrame.Angles(math.rad(90), 0, 0), 0.2)
	end
end)
1 Like