local function A(A,W)return W-(2*W:Dot(A))*A end function Fire(A)if Tool.Parent:IsA("\066\097\099\107\112\097\099\107")then return end local W=CFrame.new(Vector3.new(),A)local X=((W*CFrame.fromOrientation(0,0,RNG:NextNumber(0,TAU)))*CFrame.fromOrientation(math.rad(RNG:NextNumber(MIN_BULLET_SPREAD_ANGLE,MAX_BULLET_SPREAD_ANGLE)),0,0)).LookVector local Z=Tool.Parent:WaitForChild("\072\117\109\097\110\111\105\100\082\111\111\116\080\097\114\116",1)local p=Z.Velocity local u=X*BULLET_SPEED local L=Caster:Fire(FirePointObject.WorldPosition,X,u,CastBehavior)PlayFireSound()end local function W(A,W)local X=Instance.new("\069\120\112\108\111\115\105\111\110")X.BlastPressure=30000*W X.BlastRadius=2 X.Visible=false X.ExplosionType=Enum.ExplosionType.NoCraters X.DestroyJointRadiusPercent=0 X.Position=A X.Parent=workspace.Terrain Debris:AddItem(X,.1)end


local function Reflect(surfaceNormal, bulletNormal)
	return bulletNormal - (2 * bulletNormal:Dot(surfaceNormal) * surfaceNormal)
end

function Fire(direction)
	-- Called when we want to fire the gun.
	if Tool.Parent:IsA("Backpack") then return end -- Can't fire if it's not equipped.
	-- Note: Above isn't in the event as it will prevent the CanFire value from being set as needed.
	
	-- UPD. 11 JUNE 2019 - Add support for random angles.
	local directionalCF = CFrame.new(Vector3.new(), direction)
	-- Now, we can use CFrame orientation to our advantage.
	-- Overwrite the existing Direction value.
	local direction = (directionalCF * CFrame.fromOrientation(0, 0, RNG:NextNumber(0, TAU)) * CFrame.fromOrientation(math.rad(RNG:NextNumber(MIN_BULLET_SPREAD_ANGLE, MAX_BULLET_SPREAD_ANGLE)), 0, 0)).LookVector
	
	-- UPDATE V6: Proper bullet velocity!
	-- IF YOU DON'T WANT YOUR BULLETS MOVING WITH YOUR CHARACTER, REMOVE THE THREE LINES OF CODE BELOW THIS COMMENT.
	-- Requested by https://www.roblox.com/users/898618/profile/
	-- We need to make sure the bullet inherits the velocity of the gun as it fires, just like in real life.
	local humanoidRootPart = Tool.Parent:WaitForChild("HumanoidRootPart", 1)	-- Add a timeout to this.
	local myMovementSpeed = humanoidRootPart.Velocity							-- To do: It may be better to get this value on the clientside since the server will see this value differently due to ping and such.
	local modifiedBulletSpeed = (direction * BULLET_SPEED)-- + myMovementSpeed	-- We multiply our direction unit by the bullet speed. This creates a Vector3 version of the bullet's velocity at the given speed. We then add MyMovementSpeed to add our body's motion to the velocity.
	
	local simBullet = Caster:Fire(FirePointObject.WorldPosition, direction, modifiedBulletSpeed, CastBehavior)
	-- Optionally use some methods on simBullet here if applicable.
	
	-- Play the sound
	PlayFireSound()
end

local function KnockBack(hitPos, impactStrength)
	local boom = Instance.new("Explosion")
	boom.BlastPressure = 30000*impactStrength
	boom.BlastRadius = 2
	boom.Visible = false
	boom.ExplosionType = Enum.ExplosionType.NoCraters
	boom.DestroyJointRadiusPercent = 0
	boom.Position = hitPos
	boom.Parent = workspace.Terrain
	Debris:AddItem(boom, .1)
end
