Plataformas de Impulso
Lição 10 de 21 do curso Jogo de Roblox: Torre Infinita da Coddy.
A fase Bounce tem uma plataforma verde, marcada como Bounce, abaixo de uma plataforma elevada alta demais para pular até ela. Ao aterrissar na plataforma, você deve ser lançado para cima, até ela.
Um personagem se move com seu HumanoidRootPart, a parte invisível no seu centro. Defina o AssemblyLinearVelocity dessa parte e o personagem voará nessa direção; depois, a gravidade o trará de volta para baixo:
root.AssemblyLinearVelocity = Vector3.new(0, 60, 0) -- 60 studs por segundo, diretamente para cimaTouched fornece o membro que atingiu a plataforma, geralmente uma perna. O pai dele é o personagem, que contém o HumanoidRootPart.
Desafio
MédioFaça a plataforma verde lançar você até a saliência.
- Escreva
bounce(hit): sehit.Parenttiver umHumanoidRootPart, definaAssemblyLinearVelocitycomoVector3.new(0, Settings.BouncePower, 0). - Em
setupStage, conecte o evento Touched de cada peça marcada comBouncea essa função.
Pressione “Jogar” e pise na plataforma verde: ela lança você para cima, ao lado da saliência. Em seguida, pressione “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 TweenService = game:GetService("TweenService")
local Settings = require(game.ReplicatedStorage.Settings)
local stages = ServerStorage.Stages
local tower = nil
print("Each tower has " .. Settings.StageCount .. " stages")
-- Todo jogador recebe Wins no placar de líderes
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
-- Uma plataforma que desaparece fica transparente logo depois que alguém pisa nela 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
-- Um laser desliza até o deslocamento Slide e volta, para sempre
local function makeSlide(part)
local info = TweenInfo.new(Settings.SlideTime, Enum.EasingStyle.Linear, Enum.EasingDirection.InOut, -1, true)
local goal = { Position = part.Position + part:GetAttribute("Slide") }
TweenService:Create(part, info, goal):Play()
end
-- Lição 10: Bounce Pads vai aqui
-- 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 fase conforme 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
if part:HasTag("Slider") then
makeSlide(part)
end
end
end
-- Uma fase 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()
Todas as lições de Jogo de Roblox: Torre Infinita
2Obstáculos
O GiradorPratique por conta própria: Playground de Roblox