Menu
CoddyTech

Láseres deslizantes

Lección 8 de 18 del curso Juego de Roblox: Torre infinita de Coddy.

La etapa Lasers tiene dos paredes rojas, etiquetadas Slider y Kill, que se deslizan por el pasillo y vuelven. Cada una tiene un atributo, Slide, un Vector3 que indica adónde va desde su posición inicial.

Una interpolación puede repetirse. TweenInfo.new recibe el tiempo, la función de suavizado, una dirección, cuántas veces se debe repetir (-1 significa para siempre) y si se debe reproducir 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 una velocidad constante, para que el jugador pueda calcularla. El suavizado no predeterminado reduce la velocidad en cada extremo; se ve fluido, pero es difícil calcular el momento.

challenge icon

Desafío

Intermedio
  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, con Linear, repitiéndose indefinidamente y revirtiéndose.
  3. En setupStage, llámala para cada pieza etiquetada como Slider.

Pulsa Play y esquiva los láseres. Después, pulsa Check.

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 objeto con la etiqueta 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

-- Los objetos giratorios rotan 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

-- Elige una etapa 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()
quiz iconPonte a prueba

Esta lección incluye un breve cuestionario. Empieza la lección para responderlo y registrar tu progreso.

Todas las lecciones de Juego de Roblox: Torre infinita