Help with Spread Script

Hello. I am making Collideable Particles Module. I wanted to make script that sets Speed Direction Based on Spread but i have no idea how to do it. For now it uses a really bad spread script that works horribly. I want to make spread script that will look the same as ParticleEmitter Instance spread. If someone knows math behind this, i will be really grateful.

local random = Random.new();

--// emissionOrigin: The point of particle emission
--// emissionDirection: A unit vector of the direction you want to emit towards
--// minAngle: The minimum angle in degrees
--// maxAngle: The maximum angle in degrees
function getSpreadDirection(emissionOrigin, emissionDirection, minAngle, maxAngle)
	local x = random:NextNumber(minAngle, maxAngle);
	local y = random:NextNumber(minAngle, maxAngle);
	local angle = CFrame.Angles(math.rad(x), math.rad(y), 0);
	local cf = CFrame.lookAt(emissionOrigin, emissionOrigin + emissionDirection) * angle;
	
	return cf.LookVector;
end

local emitter = workspace.EmitterPart;
local particlePart = workspace.Part;

local direction = getSpreadDirection(emitter.Position, emitter.CFrame.LookVector, -15, 15);
particlePart:ApplyImpulse(direction * 50);

I apologize for I’m not the best at explaining.
This function above gives you a unit vector which can be used to make a part go in a spreaded direction given the desired arguments. You need a position to emit from, and a direction from that position.

Think of it like selecting Front/Back/Left/Right/Top/Bottom emission directions for a ParticleEmitter. You’d use a parts LookVector for front emission, the UpVector for top, the RightVector for right, and the negatives of them if you want to do the opposite.

The example below the function makes a direction using the emitting part’s position and it’s LookVector with a min/max angle of 15 degrees. This will get a spread direction that is within 15 degrees of the front of the part. Then all you need to do is multiply that unit vector to make it however fast you like, which in this example is 50.

Let me know if I need to clarify anything, I’ll do my best.

1 Like

Thanks for help. I will test if it works when i will have free time. I really appreciate your help.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.