加速垫
本课属于 Coddy Lua 学习旅程的“Roblox 游戏脚本编写”单元。第 18 课,共 25 课。
Humanoid 控制角色的移动方式。它的两个属性:
WalkSpeed:每秒移动的 studs,默认值为16JumpHeight:角色跳跃的高度,单位为 studs,默认值为7.2
触碰时更改这些属性就能制作强化道具。这个垫子会让踩到它的人减速:
local mud = script.Parent
mud.Touched:Connect(function(hit)
local humanoid = hit.Parent:FindFirstChild("Humanoid")
if humanoid then
humanoid.WalkSpeed = 8
end
end)这个更改会一直生效,直到有其他内容再次设置该值,或角色重新生成:新角色会从默认值开始。
挑战
中等让绿色踏板成为加速垫,这样你就能在 2.2 秒内到达黄线。
- 在 Touched 函数中,找到
hit.Parent中的Humanoid。 - 如果找到了,就将它的
WalkSpeed设为50。掉落的球没有 Humanoid,不能因此产生错误。
按“开始”,跑过踏板:你会加速冲向黄线。然后按“检查”。
亲手试试
local pad = script.Parent
pad.Touched:Connect(function(hit)
-- 找到 Humanoid,让它以 50 的速度奔跑
end)
Roblox 游戏脚本编写中的所有课
自己练习: Lua 在线编译器