Dynamic crosshair: How to convert an angle into pixels on screen?

Hi!

I have recently been making a gungame, and i am struggling to make a crosshair for the guns.

When i shoot the gun, it raycasts from the camera to where the mouse, with a random spread. This spread is determined with MATH, but has a variable. In the pic below, let us say the spread is 2. That would mean it shoots at a 2 degree angle, somewhere on the circle.

(Circle is visual only, not accurate)

How can i convert the angle of which the gun shoots at, to pixels on a screen.?

The reason I want this is to adjust the crosshairs size to visualise where the bullet can go.

What i have tried so far is to manually get what the pixel size is at X degrees and then based off that scale it, the problem is if i were to scale the users FOV (such as zooming with a scope), then it doesnt work. For some reason FOV doesnt seem to have a pattern as to how it zooms the camera. Here are datapoints on what.

NOTE: These are tests using simply my eyes to see if it fits, not 100% accurate

1 Like

I found a solution to my problem.

This function sets a point a distance away from the player. By using WorldToScreenPoint(), you can extract the radius of the circle in pixels!

local function CalcSize(currentspread, camera)

	local cam = CFrame.new(Vector3.new(), camera.CFrame.LookVector)
	local spreaddirection = CFrame.fromOrientation(0, 0, 0)
	local spreadAngle = CFrame.fromOrientation(math.rad(currentspread),0,0)
	
	local direction = (cam * spreaddirection * spreadAngle).LookVector * 1000	

	return (camera.CFrame.Position + direction)
end

local radius = CalcSize(currentspread, camera)
local vector = camera:WorldToScreenPoint(radius)