How to make camera relative character rotation?

Wassup ya’ll hope you’re having a fine morning/afternoon. I’m going to get straight to the point, how do I make the player character rotate based on camera position Y, but smoothly? I have made something relatively close but its choppy because I am using CFrame angles. Thanks in adavance!

WHAT I WOULD LIKE IT TOO LOOK LIKE:


WHAT IT LOOKS LIKE:

MY CODE:

local humanoidRootPart = player.Character:WaitForChild("HumanoidRootPart")
local head = player.Character:WaitForChild("Head")

game:GetService("RunService").RenderStepped:Connect(function()
	local camRender =  camera:GetRenderCFrame()
	local x,y,z = camRender:ToOrientation()
	local diff = humanoidRootPart.Orientation.Y - math.deg(y)
	if (diff > 45 and diff < 315) then 
		humanoidRootPart.CFrame = CFrame.new(humanoidRootPart.Position) * CFrame.Angles(0, y, 0)
	elseif (diff < -45 and diff > -315) then
		humanoidRootPart.CFrame = CFrame.new(humanoidRootPart.Position) * CFrame.Angles(0, y, 0)
	end
end)
1 Like

A quick google search usually helps.

That’s not what im looking for silly goose.

Why not orientate the head based on CFrame.fromAxisAngle with the camera? I think that’s how it works.

Wait my fault I think it’s actually a method ToAxisAngle

The head is already being orientated silly, did you watch the videos? I just want the humanoid root part to rotate if the camera is a certain distance from its original position like in the minecraft video.

Oh I should have read the post more in depth, my bad.

Maybe you can use Lerp? I think that’s a simple approach. It shouldn’t harm much to lerp on axis.

But I dont want it to do the full 45 degree angle rotation. I just want the player to correct its position.

Instead of just setting the CFrame to its new value, use the TweenSerivce it’ll provide a more smooth transition

I think I know what you mean now, you want that realistic character orientation so that if you turn to look left, the body will remain in it’s previous direction IF that direction is less than or is 45 degrees. I think that in itself should solve the issue.

more than. If the player camera has reached a certain degree, I want the character to start moving with the camera. Then when its less than it doesn’t move the character.

This seemed to work for me.
Tweak it so it works with your script.

local character = script.Parent
local humanoidRootPart = character:WaitForChild("HumanoidRootPart")
local head = character:WaitForChild("Head")

game:GetService("RunService").RenderStepped:Connect(function()
	local camRender = workspace.CurrentCamera.CFrame
	local x, y, z = camRender:ToOrientation()
	local diff = humanoidRootPart.Orientation.Y - math.deg(y)
	if (diff > 45 and diff < 315) or (diff < -45 and diff > -315) then 
		humanoidRootPart.CFrame = humanoidRootPart.CFrame:Lerp(CFrame.new(humanoidRootPart.Position) * CFrame.Angles(0, y, 0), .1)
	end
end)
2 Likes

Here how the code would look like:

repeat task.wait() until player.Character

local TweenService = game:GetService("TweenService")

local tween_info = TweenInfo.new(
	.2,
	Enum.EasingStyle.Linear,
	Enum.EasingDirection.In,
	0,
	false,
	0
)

local humanoidRootPart = player.Character:WaitForChild("HumanoidRootPart")
local head = player.Character:WaitForChild("Head")

game:GetService("RunService").RenderStepped:Connect(function()
	local camRender =  camera:GetRenderCFrame()
	local x,y,z = camRender:ToOrientation()
	local diff = humanoidRootPart.Orientation.Y - math.deg(y)
	if (diff > 45 and diff < 315) then 
		TweenService:Create(humanoidRootPart, tween_info, {
			CFrame = CFrame.new(humanoidRootPart.Position) * CFrame.Angles(0, y, 0)
		}):Play()
	elseif (diff < -45 and diff > -315) then
		TweenService:Create(humanoidRootPart, tween_info, {
			CFrame = CFrame.new(humanoidRootPart.Position) * CFrame.Angles(0, y, 0)
		}):Play()
	end
end)

I tested it, and it worked fine, see if this is what you want (you can also edit some settings to be more like you want)

1 Like

This works great! Thank you very much.

1 Like