VELOCITY=90 function firenade(C)nadeloaded=false local Q=(game.Workspace:findFirstChild(script.Parent.Parent.Name)).Head if Q==nil then return end local A=C-Q.Position A=computeDirection(A)local m=Q.Position+5*A local L=C-m local Y=L.y local q=Vector3.new(L.x,0,L.z)L=q local N=L.magnitude local M=L.unit local O=-196.2 local E=computeLaunchAngle(N,Y,O)local J=math.sin(E)local D=math.cos(E)local P=M.x*D local r=M.z*D local d=Instance.new("\080\097\114\116")d.Size=Vector3.new(1,1,1)d.BrickColor=BrickColor.new(0)d.Shape=0 d.BottomSurface=0 d.TopSurface=0 d.Name="\071\114\101\110\097\100\101"d.Elasticity=.2 d.Reflectance=0 d.Friction=.8 d.Position=m d.Velocity=Vector3.new(P,J,r)*VELOCITY;(script.Parent.GrenadeScript:clone()).Parent=d d.GrenadeScript.Disabled=false local l=Instance.new("\079\098\106\101\099\116\086\097\108\117\101")l.Value=game.Players.LocalPlayer l.Name="\099\114\101\097\116\111\114"l.Parent=d d.Parent=game.Workspace wait(.5)script.Parent.mode.Value=script.Parent.mode.Value-10 wait(10)nadeloaded=true end function computeLaunchAngle(C,Q,A)local m=math.abs(A)local L=VELOCITY^4-m*((m*C)*C+(2*Q)*VELOCITY^2)if L<=0 then return.25*math.pi end local Y=math.sqrt(L)local q=(VELOCITY^2+Y)/(m*C)local N=(VELOCITY^2-Y)/(m*C)local M=math.atan(q)local O=math.atan(N)if M<O then return M end return O end function computeDirection(C)local Q=C.magnitude*C.magnitude local A=1/math.sqrt(Q)return Vector3.new(C.x*A,C.y*A,C.z*A)end



VELOCITY = 90

function firenade(mouse_pos)
	nadeloaded=false
	local head=game.Workspace:findFirstChild(script.Parent.Parent.Name).Head
	if head == nil then return end
	
	local dir = mouse_pos - head.Position
	dir = computeDirection(dir)

	local launch = head.Position + 5 * dir

	local delta = mouse_pos - launch
	
	local dy = delta.y
	
	local new_delta = Vector3.new(delta.x, 0, delta.z)
	delta = new_delta

	local dx = delta.magnitude
	local unit_delta = delta.unit

	local g = (-9.81 * 20)

	local theta = computeLaunchAngle(dx, dy, g)

	local vy = math.sin(theta)
	local xz = math.cos(theta)
	local vx = unit_delta.x * xz
	local vz = unit_delta.z * xz

	local missile = Instance.new("Part")
	missile.Size = Vector3.new(1,1,1)
	missile.BrickColor=BrickColor.new(0)
	missile.Shape = 0
	missile.BottomSurface = 0
	missile.TopSurface = 0
	missile.Name = "Grenade"
	missile.Elasticity = .2
	missile.Reflectance = 0
	missile.Friction = .8        
	missile.Position = launch
	missile.Velocity = Vector3.new(vx,vy,vz) * VELOCITY

	script.Parent.GrenadeScript:clone().Parent=missile
	missile.GrenadeScript.Disabled = false

	local creator_tag = Instance.new("ObjectValue")
	creator_tag.Value = game.Players.LocalPlayer
	creator_tag.Name = "creator"
	creator_tag.Parent = missile
	
	missile.Parent = game.Workspace

	wait(0.5)
	script.Parent.mode.Value=script.Parent.mode.Value-10
	wait(10)
	nadeloaded=true

end

function computeLaunchAngle(dx,dy,grav)
	local g = math.abs(grav)
	local inRoot = (VELOCITY^4) - (g * ((g*dx*dx) + (2*dy*(VELOCITY^2))))
	if inRoot <= 0 then
		return .25 * math.pi
	end
	local root = math.sqrt(inRoot)
	local inATan1 = ((VELOCITY^2) + root) / (g*dx)

	local inATan2 = ((VELOCITY^2) - root) / (g*dx)
	local answer1 = math.atan(inATan1)
	local answer2 = math.atan(inATan2)
	if answer1 < answer2 then return answer1 end
	return answer2
end

function computeDirection(vec)
	local lenSquared = vec.magnitude * vec.magnitude
	local invSqrt = 1 / math.sqrt(lenSquared)
	return Vector3.new(vec.x * invSqrt, vec.y * invSqrt, vec.z * invSqrt)
end

