传送带
Coddy Roblox 游戏:无尽高塔 课程的第 8 课,共 21 课。
传送带单元里有一条深色传送带,标记为 Conveyor,它会把你往来时的方向推回去。锚定的部件不会移动,但给它设置 AssemblyLinearVelocity 后,站在上面的东西就会随之移动,就像站在真正的传送带上一样。
它朝哪个方向移动?每个部件都有正面,part.CFrame.LookVector 是一个长度为 1 个 stud、从部件正面指向外的 Vector3。传送带经过旋转,正面朝向走道的另一端:
part.AssemblyLinearVelocity = part.CFrame.LookVector * Settings.BeltSpeed使用部件自身的方向,意味着建造者可以把传送带转向任意方向,而它仍然能正常工作,无需更改脚本。
挑战
中等让传送带把你推回走道。
- 在
setupStage中,找到所有带有Conveyor标签的零件。 - 将它的
AssemblyLinearVelocity设置为part.CFrame.LookVector * Settings.BeltSpeed。
按下“开始”,站在深色传送带上:它会把你送回来的方向。然后点击“检查”。
亲手试试
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")
-- 每位玩家的排行榜上都有 Wins
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)
-- 碰到带有 Kill 标签的物体就会失去一条命
local function killOnTouch(hit)
local humanoid = hit.Parent:FindFirstChild("Humanoid")
if humanoid then
humanoid.Health = 0
end
end
-- 有人踩上会消失的台阶后,台阶很快会变淡,随后恢复原样
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
-- 旋转障碍物每帧都会转动一点
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)
-- 根据标签启用关卡中的每个障碍物
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
-- 第 8 课:Conveyor Belts 的代码写在这里
end
end
-- 随机选择一个关卡
local function pickStage()
local all = stages:GetChildren()
return all[math.random(1, #all)]
end
-- 将模型的副本放在塔顶,返回副本和新的塔顶高度
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
-- 从下往上搭建一座新的随机塔
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()
Roblox 游戏:无尽高塔中的所有课
自己练习: Roblox 在线编辑器