Turning Part To Match Camera Rotation

Im trying to make it so a part will have the same Y orientation as the Y of the camera but weird inversion happens and im really confused…

Server Code:

game:GetService("ReplicatedStorage"):WaitForChild("RotatePlayer").OnServerEvent:connect(function(player,rot)
	local players = workspace:WaitForChild("Players")
	local playermodel = players:FindFirstChild(player.Name)
	playermodel.Orientation = Vector3.new(0,rot,0)
end)

Client Code:

repeat wait() until game:IsLoaded()
repeat wait() until workspace:FindFirstChild("Players")
repeat wait() until workspace.Players:FindFirstChild(game:GetService("Players").LocalPlayer.Name)
print("game loaded")

local run_service = game:GetService("RunService")

local camera = workspace.Camera

local player = workspace.Players:FindFirstChild(game:GetService("Players").LocalPlayer.Name)

run_service.RenderStepped:connect(function()
	local x,y,z= camera.CFrame:ToEulerAnglesXYZ()
	game:GetService("ReplicatedStorage"):WaitForChild("RotatePlayer"):FireServer(y*55)
	camera.CameraSubject = player
	camera.CameraType = Enum.CameraType.Custom
end)

the *55 is just so its far enough ahead but without it the player moves about 0.1 studs.

3 Likes

I think I know what the problem is, :ToEulerAnglesXYZ() returns x, y, and z in radians, not degree’s. So you can use math.deg() to turn radians into degree’s. When you placed the orientation of the part the orientation is in degree’s. So use math.deg(), like this:

playermodel.Orientation = Vector3.new(0,math.deg(rot),0)

I just tried this and the player isnt rotating… I added back the *55 and still nothing…

Well, the player is rotating… Just veryyyy slightly

Hmm. I think you should try getting the lookvector of the camera instead and set it to the player’s orientation or cframe. And set the x and z to whatever the players orientation is originally

How about you try putting a body mover into your character and then setting the body movers cframe to the cframe of the camera and update every single frame

I would do that but im making my own physics engine and I cant use body movers as my “player” is anchored

Why cant you use CFrame instead of Orientation

image

Can we see the hierarchy of your players model?

CFrame sets position aswell… Im using a custom physics engine

image

Even if it does its not ideal to be using orientation because its not replicated

Im updating orientation on the server.

Is player model referring to the part called NonPlayer?

Yeah its called NonPlayer because its not a player yet

Any specific reason why your passing along the rotation in the remote event?

So everyone in the game can see the movement?

Your not helping much with the topic rn

Your problem comes from the use of CFrame:ToEulerAnglesXYZ().
Essentially, it doesn’t actually return the orientation of the CFrame. I don’t exactly know what it returns, but it isn’t the same as just regular orientation. Just know that you probably don’t want to be using it (unless you know exactly what it returns). Instead, you should use CFrame:ToOrientation() (which is actually just the same thing as CFrame:ToEulerAnglesZYX() but the name is a lot more intuitive so I use it). In addition, the reason for it rotating so slowly, as stated above, is because it returns radians, while you want degrees. As such, you will have to use math.deg to convert it to radians.

Your final code should look like this (this is a modification of the code you posted).

repeat wait() until game:IsLoaded()
repeat wait() until workspace:FindFirstChild("Players")
repeat wait() until workspace.Players:FindFirstChild(game:GetService("Players").LocalPlayer.Name)
print("game loaded")

local run_service = game:GetService("RunService")

local camera = workspace.Camera

local player = workspace.Players:FindFirstChild(game:GetService("Players").LocalPlayer.Name)

run_service.RenderStepped:connect(function()
	local x,y,z= camera.CFrame:ToOrientation()
	game:GetService("ReplicatedStorage"):WaitForChild("RotatePlayer"):FireServer(math.deg(y))
	camera.CameraSubject = player
	camera.CameraType = Enum.CameraType.Custom
end)
5 Likes

Thank you so much. You have literally just saved me hours in minutes