Menu
CoddyTech

Láseres deslizantes

Lección 9 de 21 del curso Juego de Roblox: Torre infinita de Coddy.

La fase Lasers tiene dos paredes rojas, etiquetadas como Slider y Kill, que se deslizan a lo largo del camino y vuelven. Cada una tiene un atributo, Slide, un Vector3 que indica adónde va desde donde empieza.

Un tween puede repetirse. TweenInfo.new recibe el tiempo, la interpolación, una dirección, cuántas veces debe repetirse (-1 significa para siempre) y si debe reproducirse en 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 mantiene constante la velocidad, para que el jugador pueda calcularla. La interpolación predeterminada (Quad, Out) se ralentiza al llegar, lo que dificulta calcular el momento.

challenge icon

Desafío

Intermedio

Haz que los láseres se deslicen por el pasillo y vuelvan.

  1. Cerca de la parte superior, obtén TweenService.
  2. Escribe makeSlide(part): anima la propiedad Position de la pieza hasta su Position más su atributo Slide, durante Settings.SlideTime segundos, de forma lineal, repitiéndose para siempre (-1) y en sentido inverso.
  3. En setupStage, llámala para cada pieza etiquetada como Slider.

Pulsa «Jugar»: las dos paredes rojas se deslizan de un lado a otro a velocidad constante. Después, pulsa «Comprobar».

Pruébalo tú mismo

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")

-- Cada jugador recibe Wins en la tabla de clasificación
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)

-- Cualquier cosa etiquetada como Kill acaba con una vida
local function killOnTouch(hit)
	local humanoid = hit.Parent:FindFirstChild("Humanoid")
	if humanoid then
		humanoid.Health = 0
	end
end

-- Un escalón que desaparece se desvanece poco después de que alguien aterrice sobre él y luego 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

-- Lección 9: Los láseres deslizantes van aquí

-- Los obstáculos giratorios giran un poco en cada fotograma
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)

-- Activa todos los obstáculos de una etapa según sus etiquetas
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

-- Una etapa cualquiera, elegida al azar
local function pickStage()
	local all = stages:GetChildren()
	return all[math.random(1, #all)]
end

-- Coloca una copia de un modelo encima de la torre; devuelve la copia y la nueva altura máxima
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

-- Construye una nueva torre aleatoria de abajo arriba
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()

Todas las lecciones de Juego de Roblox: Torre infinita

Practica por tu cuenta: Playground de Roblox