Angles atan2 problem

Hello devs!
I trying to do system for calculate Z coordinate for look into camera by right surface like when beam have faceCamera.
System which i did is working only when XY angles in 0 but if not it brokes

Working variant:

while wait() do
	workspace.Part.CFrame = CFrame.new(0,5,0)*CFrame.Angles(0,0,math.rad(90-math.deg(math.atan2(workspace.CurrentCamera.CFrame.Position.X-workspace.Part.Position.X,workspace.CurrentCamera.CFrame.Position.Y-workspace.Part.Position.Y))))
end

Dont working variant(literally same but you just need change first 2 axys in cframe.angles for broke it):

while wait() do
	workspace.Part.CFrame = CFrame.new(0,5,0)*CFrame.Angles(math.rad(87),math.rad(-86),math.rad(90-math.deg(math.atan2(workspace.CurrentCamera.CFrame.Position.X-workspace.Part.Position.X,workspace.CurrentCamera.CFrame.Position.Y-workspace.Part.Position.Y))))
end


Sorry for my english

Sorry, I might have misunderstood your question. Do you want a CFrame that has it’s right toward the Camera?

If so, could you not just use CFrame.lookAt and then turn the camera that way?

Or do you want the part to look at the Position From where the camera is?

I mean i need script wich can get Z axys which gonna look at camera, like when you doing beam and doing FaceCamera beam looks at camera by Z axys and i need to create formula which gonna give me Z degrees which gonna look at camera by Z axys

2 Likes

Ah I see.
So there is a function in CFrame which is called CFrame.lookAt()

It takes two Parameters.
atPosition
lookAt
UpVector || Vector3.new(0,1,0) --default which is this by default .The last parameter is optional

Why not try:

local RunService = game:GetService("RunService")
RunService.Heartbeat:Connect(function()
    workspace.Part.CFrame = CFrame.lookAt(Vector3.new(0,5,0),workspace.CurrentCamera.CFrame.Position)
end)

This will always make the Part look towards the camera. No need for any maths .

If you want to do it the mathematical way then you probably need to make an entirly new plane get the cross product. Trigonometry technically doesn’t exist in 3 planes however you can make a new plane and work out the Z axis like that.

Again you don’t have to use the cross product since it makes things more complex.

If you want the part just to face the camera then just do that.

This is not how beam FaceCamera looks, this is just rotate part directly in camera wich i can do by myself.
At first video you can see how i need system to work(chosed surface by decal is right) but my system works if angle offset is 0,0,0 but if not it brokes

1 Like