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?
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
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.