What do you want to achieve?
I want a spread pattern of a gun to be in a circle.
What is the issue?
The spread I have is more of a square pattern than a circle.
What solutions have you tried so far?
On paper, I was thinking maybe I can try subtracting the other axis counter part by subtracting the spread amount. For example, if the spread is 2 and the rng chooses 2 on the x-axis and 2 on the y-axs, I would subtract half the amount of y with x and vice versa. However, this results in a diamond shape than a circle.
Below is the code I have for my spread rng without the mentioned theoretical solution above:
local cf = CFrame.new(self.startPosition, self.endPosition)
-- Rotate bullet by some spread (degree)
local offsetX = math.rad(RandomNumberGenerator(-self.spread, self.spread))
local offsetY = math.rad(RandomNumberGenerator(-self.spread, self.spread))
cf = cf * CFrame.Angles(offsetX, offsetY, 0)
Try this for uniform distribution in a radius ofself.spread:
local cf = CFrame.new(self.startPosition, self.endPosition)
-- Rotate bullet by some spread (degree)
local r = self.spread * math.sqrt(math.random())
local theta = math.random() * 2 * math.pi
local offsetX = r * math.cos(theta)
local offsetY = r * math.sin(theta)
cf *= CFrame.Angles(offsetX, offsetY, 0)
This code generates a random theta in polar coordinate form and then converts that result into cartesian coordinates. If you want to learn more about the math behind this, check this stack overflow answer out.