Menu
CoddyTech

弹跳垫

Coddy Roblox 游戏:无尽高塔 课程的第 10 课,共 21 课。

弹跳关卡有一个绿色垫子,标记为 Bounce,位于一道高得无法跳上的平台边缘下方。落到垫子上后,它应该会把你弹到平台上。

角色通过它的 HumanoidRootPart 移动,这是角色中间不可见的部分。设置该部分的 AssemblyLinearVelocity 后,角色会朝那个方向飞去,然后在重力作用下落下来:

root.AssemblyLinearVelocity = Vector3.new(0, 60, 0) -- 每秒 60 个 stud,竖直向上

Touched 会提供撞到垫子的肢体,通常是一条腿。它的父对象是角色,角色包含 HumanoidRootPart。

challenge icon

挑战

中等

让绿色垫子把你弹到平台边缘。

  1. 编写 bounce(hit):如果 hit.Parent 有一个 HumanoidRootPart,就将它的 AssemblyLinearVelocity 设置为 Vector3.new(0, Settings.BouncePower, 0)。
  2. 在 setupStage 中,将每个标记为 Bounce 的部件的 Touched 事件连接到它。

按“开始”,然后踩上绿色垫子:它会把你弹到平台边缘旁边。然后点击“检查”。

亲手试试

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")

-- 每位玩家的排行榜上都有 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

-- 激光会一直滑到 Slide 指定的偏移位置,再滑回来
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

-- 第 10 课:在这里添加弹跳垫

-- 旋转障碍物每帧都会转动一点
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
		if part:HasTag("Conveyor") then
			part.AssemblyLinearVelocity = part.CFrame.LookVector * Settings.BeltSpeed
		end
		if part:HasTag("Slider") then
			makeSlide(part)
		end
	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 在线编辑器