Sonic Crack simulation for weapons

I’m trying to think of the best possible way to find out if a player should hear a directional sonic crack when a weapon is fired in their direction. If the simulation is ran when the source weapon is fired would I use ents.FindInCone or is there a better way using traces?

You can use GM:EntityFireBullets to detect where a gun has been shot ( that uses bullets ). It provides the bullet structure as the data argument, so you would have the origin and the normal of the bullet.

From there, you’d just determine each player within a cone from there with something like:


local function InCone( origin, dist, dir, radius, ent )
	local pos, id, att
	
	if not ent:IsValid( ) then
		--Invalid entities are never in the cone
		return false
	end
	
	pos = ent:NearestPoint( origin )
	
	if pos:Distance( origin ) > dist then
		--If we are outside the radius, return false
		return false
	end
	
	if ( 1 - dir:Dot( pos ) ) >= radius then
		--Facing the wrong way
		return false
	end
	
	return true
end


for k, v in pairs( player.GetAll( ) ) do
  if InCone( data.Src, 4096, data.Dir, math.rad( 30 / 2 ), v )
    --Do something
  end
end

You’ll need to play with radius and distance to suit your needs.

If you want to account for things in the way ( like walls or the ground ), you’ll need to do some traces.

Thanks, that helped!