Part Orientation Same As Camera

How would I make an anchored part point in the same direction as the camera? And there are a few other topics like this but the are talking about how to make a part point where the camera is looking, if that makes sense.

So basically I want a part to have the same exact orientation as the camera.

1 Like

You can get the camera orientation using workspace.CurrentCamera.CFrame:ToEulerAnglesXYZ()

Hmm… How would I put a parts rotation to this?

local RunService = game:GetService("RunService")

RunService.Heartbeat:Connect(function()
   Part.Orientation = workspace.CurrentCamera.CFrame:ToEulerAnglesXYZ()
end)
part.CFrame = Camera.CFrame - Camera.CFrame.Position + part.Position;

Or, if you want to keep it at it’s current position and make it look at the camera you can do

part.CFrame = CFrame.LookAt(part.Position, Camera.CFrame.Position)

And finally to keep the cameras exact position and orientation you can just use part.CFrame = Camera.CFrame

Keep in mind if you want it to constantly update you’d need to throw it in a loop.


However, let’s say the camera is an arrow pointing at some position in world space, and you want your part to look at that, you can use the second technique I mentioned but replace the camera’s position with the position of a Ray’s result that is cast from the center of the camera in the direction of the camera. Just make sure to account for cases where you don’t hit anything.

2 Likes

thank you so much, it works just as I wanted.

1 Like

No problem! For further reading, take a look at

https://developer.roblox.com/en-us/api-reference/datatype/CFrame

and

It took me a while to get a good understanding of CFrames so don’t fret if you struggle at first. Good luck!

CFrame.Rotation is a thing. I keep seeing people subtracting cframe by its own position. I mean shouldn’t it autocomplete and show Rotation?

image

image

1 Like

Ah, that would be better to use. Wasn’t aware CFrame.Rotation was exactly the same, thought it was just an alternative to :ToOrientation()