Menu
CoddyTech

Lasers Deslizantes

Lição 8 de 18 do curso Jogo de Roblox: Torre Infinita da Coddy.

A fase Lasers tem duas paredes vermelhas, marcadas com as tags Slider e Kill, que deslizam pela passarela e voltam. Cada uma tem um atributo, Slide, um Vector3 que indica para onde ela vai a partir do ponto inicial.

Uma interpolação pode se repetir. TweenInfo.new recebe o tempo, a função de easing, uma direção, quantas vezes repetir (-1 significa para sempre) e se deve reproduzir no sentido inverso:

local info = TweenInfo.new(2.5, Enum.EasingStyle.Linear, Enum.EasingDirection.InOut, -1, true)
TweenService:Create(part, info, { Position = part.Position + part:GetAttribute("Slide") }):Play()

Linear mantém a velocidade constante, permitindo que o jogador a avalie. O easing padrão desacelera em cada extremidade, o que parece suave, mas dificulta calcular o momento certo.

challenge icon

Desafio

Médio
  1. Perto do início, obtenha TweenService.
  2. Escreva makeSlide(part): faça um tween da Position da peça até sua Position mais o atributo Slide, levando Settings.SlideTime segundos, com Linear, repetindo para sempre e revertendo.
  3. Em setupStage, chame essa função para cada peça com a tag Slider.

Pressione Jogar e passe pelos lasers. Depois, Verificar.

Experimente você mesmo

local Players = game:GetService("Players")
local ServerStorage = game:GetService("ServerStorage")
local CollectionService = game:GetService("CollectionService")
local RunService = game:GetService("RunService")
local Settings = require(game.ReplicatedStorage.Settings)

local stages = ServerStorage.Stages
local tower = nil

print("Each tower has " .. Settings.StageCount .. " stages")

-- Todos os jogadores recebem Wins no placar
Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player

	local wins = Instance.new("IntValue")
	wins.Name = "Wins"
	wins.Parent = leaderstats
end)

-- Qualquer coisa com a tag Kill tira uma vida
local function killOnTouch(hit)
	local humanoid = hit.Parent:FindFirstChild("Humanoid")
	if humanoid then
		humanoid.Health = 0
	end
end

-- Um degrau que desaparece fica translúcido logo após alguém pisar nele e depois reaparece
local function makeVanish(part)
	part.Touched:Connect(function(hit)
		if part:GetAttribute("Gone") or not hit.Parent:FindFirstChild("Humanoid") then
			return
		end
		part:SetAttribute("Gone", true)
		task.wait(Settings.VanishDelay)
		part.Transparency = 0.8
		part.CanCollide = false
		task.wait(Settings.VanishTime)
		part.Transparency = 0
		part.CanCollide = true
		part:SetAttribute("Gone", nil)
	end)
end

-- Os Spinners giram um pouco a cada quadro
RunService.Heartbeat:Connect(function(dt)
	for _, spinner in CollectionService:GetTagged("Spinner") do
		if spinner:IsDescendantOf(workspace) then
			spinner:PivotTo(spinner:GetPivot() * CFrame.Angles(0, math.rad(Settings.SpinSpeed) * dt, 0))
		end
	end
end)

-- Ativa todos os obstáculos de uma etapa de acordo com suas tags
local function setupStage(stage)
	for _, part in stage:GetDescendants() do
		if part:HasTag("Kill") then
			part.Touched:Connect(killOnTouch)
		end
		if part:HasTag("Vanish") then
			makeVanish(part)
		end
		if part:HasTag("Conveyor") then
			part.AssemblyLinearVelocity = part.CFrame.LookVector * Settings.BeltSpeed
		end
	end
end

-- Uma etapa qualquer, escolhida aleatoriamente
local function pickStage()
	local all = stages:GetChildren()
	return all[math.random(1, #all)]
end

-- Coloca uma cópia de um modelo no topo da torre; retorna a cópia e a nova altura do topo
local function stack(template, bottom)
	local copy = template:Clone()
	local box, size = copy:GetBoundingBox()
	local lift = bottom - (box.Position.Y - size.Y / 2)
	copy:PivotTo(copy:GetPivot() + Vector3.new(0, lift, 0))
	copy.Parent = tower
	return copy, bottom + size.Y
end

-- Constrói uma nova torre aleatória de baixo para cima
local function buildTower()
	tower = Instance.new("Model")
	tower.Name = "Tower"
	tower.Parent = workspace
	local top = 0
	for i = 1, Settings.StageCount do
		local stage, newTop = stack(pickStage(), top)
		top = newTop
		setupStage(stage)
	end
	stack(ServerStorage.Finish, top)
end

buildTower()
quiz iconTeste seus conhecimentos

Esta lição inclui um quiz rápido. Comece a lição para respondê-lo e acompanhar seu progresso.

Todas as lições de Jogo de Roblox: Torre Infinita