I’m doing a crosshair for my gun, but I don’t get what I want to do because I suck at math
What I am trying to do is a function that gives me a direction towards a random point within the crosshair BUT🍑 that is from a basepart towards the random point in 3D, because the weapon is in third person
local camera = game.Workspace.CurrentCamera
local maxSpread = 20-- degrees
local function getRandomAngle(spread)
spread *= 10
return math.deg(math.random(-spread, spread) / 10)
end
local function getRandomPointInCrosshair()
local crosshairRay = camera:ViewportPointToRay()
local crosshairCFrame = CFrame.lookAt(crosshairRay.Origin, crosshairRay.Origin + crosshairRay.Direction)
crosshairCFrame *= CFrame.Angles(0, getRandomAngle(maxSpread), getRandomAngle(maxSpread))
local result = game.Workspace:Raycast(randomizedCameraAngle.Position, randomizedCameraAngle.lookVector * 300)
if result then
return result.Position
end
end
edit: if it doesn’t spread vertically swap out the values for z from CFrame.Angles with the x instead
(replied to myself lol)
it’s a function in the code snippet, it’s declared there
btw the way it’s written means the spread is gonna be relative to how far your camera is to your target (if I understood the example pic correctly), if your intent was instead to have it be relative to the shotgun muzzle lmk since we’d need to swap out a couple variables
local function GetSpreadDirection(Offset)
local RA = Offset * math.sqrt(math.random()) / 2.5
local theta = math.random() * 2 * math.pi
local XOff = RA * math.cos(theta)
local YOff = RA * math.sin(theta)
local CrossHairPos = camera:ScreenPointToRay(mouse.X + XOff, mouse.Y + YOff)
return CrossHairPos.Direction
end