How to rotate orientation on one axis only using CFrame.LookAt

Hiya! I’ve been wanting to make a top down camera script and it’s been pretty simple so far but I hit a roadblock and that is that I need to only rotate the players humanoidrootpart on the Y axis but using CFrame.LookAt it’s rotating to look at the mouse on all axis.

The player rotation code:

RS.RenderStepped:Connect(function()
	camera.CFrame = (char.HumanoidRootPart.CFrame + offset) * CFrame.Angles(math.rad(-90), 0, 0)
	
	char.HumanoidRootPart.CFrame = CFrame.lookAt(char.HumanoidRootPart.Position, mouse.Hit.p) -- rotation on all axis!
end)

I would really appreciate help with this!

Hello.

I had actually done something similar and this is what worked for me.

CFrame.new(HumanoidRootPart.Position,Vector3.new(Mouse.hit.Position.X, HumanoidRootPart.Position.Y, Mouse.hit.Position.Z))

2 Likes

This works! Only problem I have now is that the player spins very quickly. Any idea on how to fix this?

You can make a tween and make the speed to your choice. (The number inside TweenInfo.new() )

local TweenService = game:GetService("TweenService")

local RootRotationTween = TweenService:Create(HumanoidRootPart,TweenInfo.new(1),{CFrame = CFrame.new(HumanoidRootPart.Position,Vector3.new(Mouse.hit.Position.X, HumanoidRootPart.Position.Y, Mouse.hit.Position.Z))})
RootRotationTween:Play()

You have to put the last two lines in the loop since the tween being created would need to use the current updated Root position and Mouse position.

1 Like

Thank you so much for the well thought out reply but I actually found the problem! I used CFrame.new() instead and that seemed to fix the problem.

camera.CFrame = CFrame.new(char.HumanoidRootPart.Position + offset) * CFrame.Angles(math.rad(-90), 0, 0)

Funny thing is, now the player isn’t moving lol

Nvm fixed that! I just changed the rotation of the camera from -90 to -89. This seemed to do the job… I’m still kinda confused why it fixed it though.

1 Like