I have a weapons system in a game with a shoulder cam. The game currently raycasts a line from the “center” of the screen outwards to the direction the screen is looking at. When you aim at the sky the raycast will shoot straight down. This is obviously because there is nothing for the raycast to hit therefor it returns nil. However I don’t wan’t to just disable the players ability to shoot. I have looked at a couple of posts involving the same issue but none of their fixes worked for mine. I tried just having the hitposition of the raycast be equal to the direction + maxbullet distance and direction + origin and some variance of setting the frame from the origin to lookat the direction point. Here is the code
local random = Random.new()
local resolution = camera.ViewportSize
local random = Random.new()
local resolution = camera.ViewportSize
local center = UDim2.fromOffset(resolution.X / 2, resolution.Y / 2.4)
--the 2.4 is because for whatever reason the ray will shoot lower than the center off the crossair
local unitRay = camera:ScreenPointToRay(center.X.Offset, center.Y.Offset)
local origin = unitRay.Origin
local direction = unitRay.Direction
local cFrame = CFrame.lookAt(origin, origin + direction)
* CFrame.Angles(0, 0, random:NextNumber(0, 2 * math.pi))
* CFrame.Angles(0, random:NextNumber(0, math.rad(self.Spread.Value / 10)), 0)
direction = cFrame.LookVector
return origin, direction