Issue with "mounting" part to camera

I’ve got a script that sets a parts position infront of the camera ever frame, and that works fine, but I have a way of using ScreenPointToRay to get the spot on screen in which I want to put the part. The problem with this is that I cannot get the UI to always be facing the camera. This is my code, and a gif of the problem.

local function RenderStepped()
	local ScreenRay = Camera:ScreenPointToRay(15, Camera.ViewportSize.Y -15, 7)
	UIPart:PivotTo(CFrame.lookAt(ScreenRay.Origin, ScreenRay.Direction)) 
end

GIF: Image from Gyazo

(The part should appear stationary, but instead it rotates. The position itself seems fine, it’s just the rotation.)

Hello Skip, try setting the parts cframe to the camera and offsetting it from there it doesn’t need any
ray-casting.

Hi, thanks for the reply. This is what I originally did, 100% worked, the problem comes when I try to position the part somewhere that isn’t the centre of the screen, that’s why I used ScreenPointToRay to get a spot to place the part on that part of the screen. There may be some alternative to ScreenPointToRay that I’m not aware of though. The part needs to be in about the same spot on all resolutions, which is why I used ScreenPointToRay as well.

Have you tried using ViewportFrames instead?

ViewportFrames would not be suitable for what I’m doing.

The CFrame.lookAt() constructor results in the part’s orientation changing.

CFrame.new(CFrame.lookAt(ScreenRay.Origin, ScreenRay.Direction).Position)

If you just want a CFrame value which describes a world space position and not a world space orientation.

1 Like

Thanks, that worked for the most part, but the only problem is that it doesn’t rotate with the camera. I tried doing this: CFrame.new(CFrame.lookAt(ScreenRay.Origin, ScreenRay.Direction).Position) * CFrame.Angles(math.rad(-5), math.rad(5), math.rad(0)) but got this result: Image from Gyazo