Bệ nảy
Bài học 10 trên 21 trong khóa học Trò chơi Roblox: Tháp bất tận của Coddy.
Phần Bounce có một tấm đệm màu xanh lá, gắn thẻ Bounce, nằm bên dưới một gờ quá cao để nhảy lên. Khi tiếp đất trên tấm đệm, nhân vật sẽ được hất lên gờ.
Nhân vật di chuyển bằng HumanoidRootPart, bộ phận vô hình ở giữa cơ thể. Đặt AssemblyLinearVelocity của bộ phận đó, nhân vật sẽ bay theo hướng ấy, rồi trọng lực kéo nhân vật xuống:
root.AssemblyLinearVelocity = Vector3.new(0, 60, 0) -- 60 stud mỗi giây, thẳng lên trênTouched cung cấp cho bạn bộ phận cơ thể chạm vào tấm đệm, thường là chân. Bộ phận đó có đối tượng cha là nhân vật, nơi chứa HumanoidRootPart.
Thử thách
Trung bìnhKhiến đệm màu xanh lá hất bạn lên gờ.
- Viết
bounce(hit): nếuhit.ParentcóHumanoidRootPart, đặtAssemblyLinearVelocitycủa nó thànhVector3.new(0, Settings.BouncePower, 0). - Trong
setupStage, kết nối Touched của mọi bộ phận được gắn thẻBouncevới hàm này.
Nhấn Chơi rồi bước lên đệm màu xanh lá: đệm sẽ hất bạn lên cạnh gờ. Sau đó nhấn Kiểm tra.
Tự làm thử
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")
-- Mỗi người chơi đều có Wins trên bảng xếp hạng
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)
-- Chạm vào bất cứ thứ gì gắn thẻ Kill sẽ mất một mạng
local function killOnTouch(hit)
local humanoid = hit.Parent:FindFirstChild("Humanoid")
if humanoid then
humanoid.Health = 0
end
end
-- Bậc thang biến mất sẽ mờ đi ngay sau khi có người đáp xuống, rồi xuất hiện trở lại
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
-- Tia laser trượt đến vị trí lệch Slide rồi quay lại, lặp mãi
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
-- Bài học 10: Bounce Pads nằm ở đây
-- Các vật xoay quay một chút sau mỗi khung hình
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)
-- Kích hoạt mọi chướng ngại vật trong một tầng dựa trên thẻ của chúng
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
-- Chọn ngẫu nhiên một tầng bất kỳ
local function pickStage()
local all = stages:GetChildren()
return all[math.random(1, #all)]
end
-- Đặt một bản sao của mô hình lên đỉnh tháp; trả về bản sao và độ cao đỉnh mới
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
-- Xây một tòa tháp ngẫu nhiên mới từ dưới lên
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()
Tất cả bài học trong Trò chơi Roblox: Tháp bất tận
Tự luyện tập: Playground Roblox