Calculate/get CFrame.Angles() to face Player

See attachment above. I’m trying to make the object/model face the player at the beginning of the placement time. Notice how it always faces a single direction no matter where and what I place.

I’ve tried using lookAt, however it doesn’t work. This is the script I’m using to get the active live rendering before placing the object down.

RunService.RenderStepped:Connect(function()
		if PlacingObject == true and PrimaryPart then
			Mouse.TargetFilter = PreviewObject
			local Hit = Mouse.Hit
			
			if PrimaryPart then
				local ObjectCFrame = CFrame.new(Hit.Position.X, Hit.Position.Y + 
  PreviewObject.PrimaryPart.Size.Y/2 + Offset, Hit.Position.Z)
				
				local Diff = ObjectCFrame.Y - Character.HumanoidRootPart.Position.Y
				local ObjectAngles = CFrame.Angles(0,math.rad(RotationAmount),math.rad(-90))
               PreviewObject:SetPrimaryPartCFrame(ObjectCFrame * ObjectAngles)

The rest of the code is cut off because it is irrelevant to this issue. Are there any ideas to get it to face the player when it starts the placement preview? Perhaps by using the calculation to store into RotationAmount. I think calculating the CFrame.Angles at the beginning would help, but I don’t know how to get the Y of the CFrame.Angles() to face the player at all.

Thank you in advance and I’ll try my best to elaborate more in comments if necessary.

I don’t understand why lookAt wouldn’t work. You need to elaborate on that. To extract the RotationAmount from that, you can do something like

local _, RotationAmount = CFrame.lookAt(sign.Position, player.HumanoidRootPart.Position):ToOrientation()
--Because RotationAmount is already in radians, we don't need to use math.rad anymore
--also math.rad(-90) is just math.pi*-.5, use that instead of redundantly calling the function

--inside RenderStepped
local ObjectAngles = CFrame.Angles(0, rotationAmount, math.pi*-.5)

I’ve implemented that into my script, however, it still has the same behavior. See the attached video for demo:

Try printing out the RotationAmount variable to validate it. Might be something else causing the problem.

you can try using

PreviewObject.CFrame = CFrame.new(PreviewObject.Position, Character.Torso.Position)

im not the best scripter so i dont know if it works but i hope it helps!
edit: i tested it and it works, it should work for you too

That’s literally the same thing as CFrame.lookAt except it’s practically deprecated

oh, i didnt know that because i watched tutorials from 2019

To get the Y value of CFrame.Angles (also known as the Yaw axis) you will have to use trigonometry:


Alpha is the angle between the 2 points denoted as (X1, Y1) and (X2, Y2).
It is in 2D, the implementation of 3D is simple because we only have to look at 2 axes (like in the 2D case), the Z and X axis. So the Y axis would just become the Z axis. In code:

local p0 = ...; -->> Whatever Vector3 position
local p1 = ...; -->> Whatever Vector3 position
local YAngle = math.atan2((p1 - p0).Z, (p1 - p0).X) -->> In radians.

I solved it based on your code, however, I used a bit of CFrameAngles and Pivots to get what I wanted. Thank you though!