CFrame:ToOrientation() issue

game:GetService("RunService").Heartbeat:Connect(function()
	local x, y, z = cam.CFrame:ToOrientation()
	char:PivotTo(CFrame.new(Sphere.Position, Vector3.new(x, y, z)))
end)

I made a script which should be moving the character to another object’s position (Sphere.Position) and it should make the character face wherever the camera is facing

It moves the character to the proper position, but it rotates them to be facing the origin (0, 0, 0) instead of facing the same direction as the camera

There’s no errors in the script, and this is my first time using CFrame:ToOrientation()

In order to set a part to a specific angle, you need to multiply the CFrame.new(position) by the angle you want to offset it by.

Here’s your altered (and untested on my end) code:

game:GetService("RunService").Heartbeat:Connect(function()
	local x, y, z = cam.CFrame:ToEulerAnglesYXZ() -- Isolate the CFrame's angles, same as ToOrientation()
	char:PivotTo(CFrame.new(Sphere.Position) * CFrame.Angles(x, y, z))
end)

This seems to work, but I’m still a little confused. What is the difference between putting in the values this way:

char:PivotTo(CFrame.new(Sphere.Position, Vector3.new(x, y, z)))

and putting in the values this way?

char:PivotTo(CFrame.new(Sphere.Position) * CFrame.Angles(x, y, z))

The way I see things, the 2nd property of the CFrame should be the orientation, so I put the camera’s orientation in there and it didn’t work

I understand why yours works, but what was wrong with the original script?

CFrame.new() has many overload methods (basically just a lot of different versions of CFrame.new()), and the one most used is CFrame.new(Vector3). But, the method you were using is passing in two Vector3 components, the first being the position, but according to the documentation the second Vector3 you passed is a LookAt position, which would cause the CFrame to look at the specified position.

You can read more about CFrame.new() here: CFrame | Roblox Creator Documentation

To fully answer your question (in simple terms), you passed a LookAt vector into the CFrame.new(), not an angle, which caused it to not function as intended.

1 Like