Can someone help me get a value from this function

I have this function, that basically adds a spread to a look vector. Can someone help me figure out how to make some value bigger or smaller depending on how far away from the center the point is?


(red being the angle/point)

	local cosAngle = math.cos(angle)
	local z = 1 - math.random()*(1 - cosAngle)
	local phi = math.random()*math.pi*2
	local r = math.sqrt(1 - z*z)
	local x = r * math.cos(phi)
	local y = r * math.sin(phi)
	local vec = Vector3.new(x, y, z)
	if axis.Z > 0.9999 then
		return vec
	elseif axis.Z < -0.9999 then
		return -vec
	end
	local orth = Vector3.zAxis:Cross(axis)
	local rot = math.acos(axis:Dot(Vector3.zAxis))
	return CFrame.fromAxisAngle(orth, rot) * vec
end

I didnt make the function, someone made it in a topic that someone showed me

1 Like

It’s probably more difficult to reverse engineer than learn it from scratch especially if the code isn’t commented.

Looking at the diagram it seems like shotgun spread or any type of spread so these two resources should help:

I tried using those links, and the shotgun one had to use the mouse.hit, which I don’t use, and the other one I didn’t understand. However, I think I might’ve found something. I might be able to convert the vector into orientation using this. I just don’t know how to get the up vector.

local cone = RandomCone(part1.CFrame.LookVector, SPREAD)
--print(cone)
local someLookVector = cone
local upVector = Vector3.new(0, 1, 0)
local cf = CFrame.lookAt(Vector3.new(), someLookVector, upVector)
local x, y, z = cf:ToOrientation()

That is false, it doesn’t need to use mouse.hit.

The function is compatible with any type of direction vector

In the code mouse.Hit was used to create a direction vector from the gun point to target mouse position.

local dir = RandomVectorOffset((mouse.Hit.p - ShootPart.CFrame.p).Unit, math.rad(30)) 
local ray = Ray.new(ShootPart.CFrame.p, dir * ShotgunConfigs.Range.Value)

This is not needed for your case scenario.

As a result, you can easily replace it with any other type of direction vector.

-local dir = RandomVectorOffset((mouse.Hit.p - ShootPart.CFrame.p).Unit, math.rad(30)) 
+local dir = RandomVectorOffset(part1.CFrame.LookVector, math.rad(30)) 
local ray = Ray.new(ShootPart.CFrame.p, dir * ShotgunConfigs.Range.Value)

Well I certainly found something out, local dotProduct = part1.CFrame.LookVector:Dot(cone).
This pretty much gives what I want, I’m just having trouble tuning it. This is what I have so far:

local dotProduct = part1.CFrame.LookVector:Dot(cone)
local scale = 0.1/(dotProduct^20)
print(scale)

The problem is, I want it to be 0 when it is in the direct center of it, but the only way is to put it to 0/(dotProduct^20), but this makes all values 0.

Seems like you can solve this on your own as you are getting closer.

I’ll just post this resource here which explains how to make a value smaller when another value increases like distance:

I also recommend using desmos for plotting the equations as well.

Also for more ideas consider the math library like below: