Băng chuyền
Bài học 8 trên 21 trong khóa học Trò chơi Roblox: Tháp bất tận của Coddy.
Băng chuyền có một dây băng tối màu, được gắn thẻ Conveyor, đẩy bạn lùi về hướng bạn vừa đi. Một bộ phận được neo sẽ không di chuyển, nhưng khi đặt AssemblyLinearVelocity cho nó, mọi thứ đứng trên đó sẽ được cuốn đi theo, giống như trên băng chuyền thật.
Theo hướng nào? Mỗi bộ phận đều có mặt trước, còn part.CFrame.LookVector là một Vector3 dài một stud, hướng ra ngoài từ bộ phận đó. Dây băng được xoay để mặt trước hướng xuống lối đi:
part.AssemblyLinearVelocity = part.CFrame.LookVector * Settings.BeltSpeedDùng hướng riêng của bộ phận nghĩa là người xây dựng có thể xoay dây băng theo bất kỳ hướng nào mà nó vẫn hoạt động, không cần thay đổi script.
Thử thách
Trung bìnhKhiến băng chuyền đẩy bạn lùi xuống lối đi.
- Trong
setupStage, tìm mọi bộ phận có gắn thẻConveyor. - Đặt
AssemblyLinearVelocitycủa nó thànhpart.CFrame.LookVector * Settings.BeltSpeed.
Nhấn Chơi rồi đứng trên băng chuyền tối màu: nó sẽ đưa bạn trở lại theo đường bạn vừa đi. 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 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 kỳ 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
-- Các vật quay xoay 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
-- Bài học 8: Băng chuyền sẽ nằm ở đây
end
end
-- Chọn ngẫu nhiên một tầng
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 tháp mới với các tầng ngẫu nhiên, 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