How to make a gun shoot at the camera

i’m trying to make a gun that shoots at the center of the player’s screen, the crosshair is located at the center. i’ve been trying and failing at the math on this for about a week, my current way to cast the ray is:

local Camera = workspace.CurrentCamera
local Ray = Camera:ViewportPointToRay(Camera.ViewportSize.X/2,Camera.ViewportSize.Y/2-game:GetService("GuiService"):GetGuiInset().Y/2)
local Direction = Ray*150

it shoots CLOSE to the center of the screen, but it seems to be different on different screens. anything i can do to fix this?

1 Like

Are you sure this is still necessary? Viewport naturally excludes the topbar inset, aka it actually does include the entire screen. You adding this offset could be the problem because there isn’t a need for it anymore

1 Like

i removed it and same results: the crosshair is accurate on some devices and not accurate on others. i’m trying to get it to shoot at the little crosshair on the screen regardless of how tall or short the screen is

I am unable to replicate your issue. You can see me resizing the screen and testing it on the display of an iPhone, the red part still perfectly matches the white dot (a UI frame) on the screen.

it’s not the crosshair, the gun isn’t shooting towards the proper crosshair
u can see in these recordings that it works perfectly fine on one device but not on the other


You forgot to disable UI inset on the crosshair’s ScreenGui itself. The crosshair literally doesn’t match up with the center of the screen.

in hindsight i should have noticed that earlier lol

thanks

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.