Setting the velocity of a particle to fire forward.

I’m trying to make a Swep that fires a smokey effect. But it’s harder to do than I thought.

This is what I have in my SWEP:PrimaryAttack()
[lua] local ply = self.Owner
local ED = EffectData()
ED:SetOrigin(ply:GetShootPos() + ply:GetForward()*28 + ply:GetRight()5 + ply:GetUp()-6)
ED:SetAngle(ply:GetForward():GetNormalized())
util.Effect(“effect_bugspray”,ED)[/lua]

And this is the effect:
[lua]function EFFECT:Init(data)

local pos = data:GetOrigin()
local ang = data:GetAngle()

local emitter = ParticleEmitter(pos)

	local X = math.Rand(-2, 2)
	local Y = math.Rand(-2, 2)
	local ppos = pos + Vector(X, Y, 0)
	local spray = emitter:Add("particle/particle_smokegrenade", ppos)
	if (spray) then
		
		spray:SetVelocity(ang)
		
		spray:SetLifeTime(0)
		spray:SetDieTime(1)
		
		local color = math.Rand(220,250)
		spray:SetColor(color, color, color)
		spray:SetStartAlpha(math.Rand(190,200))
		spray:SetEndAlpha(math.Rand(0,100))
		
		spray:SetStartSize(math.Rand(1,2))
		spray:SetEndSize(math.Rand(10,12))
		
		spray:SetRoll(math.Rand(0,360))
		spray:SetRollDelta(math.Rand(-4,4))
		
		spray:SetGravity(Vector(0,0,0))
		spray:SetAirResistance(200)
		
		spray:SetCollide(true)
		spray:SetBounce(0.5)
		
	end

emitter:Finish()

end

function EFFECT:Think()

return false

end

function EFFECT:Render()

end[/lua]

SetVelocity sets the full velocity not just the direction, so you need to multiply the direction by something. And your air resistance is probably playing a role in the movement as well.

Also, pl.GetForward already returns a unit vector. There’s no need to normalize it again.

Well with what I have now the particles fire in weird directions, if I multiply it the particles go way too fast.

Here are screenshots:

Looks like Get/SetAngle is at fault. Change it to Get/SetNormal and then multiply the velocity by something.

Thanks a ton! That fixed it.