PcoWSkbVqDnWTu_dm2ix
We use cookies on this site to enhance your user experience

Creation of a Guided Rocket Launcher: Code Sample 1

Creation of a Guided Rocket Launcher: Code Sample 1

Oct 01 2018, 2:17 AM PST

function fire(vTarget)
	local vCharacter = Tool.Parent	
	local vHandle = Tool:findFirstChild("Handle")
	if vHandle == nil then
		print("Handle not found")
		return 
	end
	local direction = vTarget - vHandle.Position
	direction = computeDirection(direction)
	local missile = Rocket:Clone()
	local pos = vHandle.Position + (direction * 10.0)		
	missile.CFrame = CFrame.new(pos,  pos + direction) * CFrame.Angles(math.pi/2, 0, 0)

	local creator_tag = Instance.new("ObjectValue")

	local vPlayer = game.Players:GetPlayerFromCharacter(vCharacter)

	if vPlayer == nil then
		print("Player not found")
	else
		if (vPlayer.Neutral == false) then -- nice touch
			missile.BrickColor = vPlayer.TeamColor
		end
	end

	local floatForce = Instance.new("BodyForce")
	floatForce.force = Vector3.new(0, missile:GetMass() * 196.1, 0.0)
	floatForce.Parent = missile

	missile.Velocity = direction * 20.0

	creator_tag.Value = vPlayer
	creator_tag.Name = "creator"
	creator_tag.Parent = missile	

	missile.Parent = game.Workspace

	if swooshSound then swooshSound:Play() end

	missile.Touched:Connect(function(hit) blow(hit, missile) end)

	debris:AddItem(missile, 100.0)
end