跳至正文
版本:bleeding-edge 🩸

道具雨

如何制作一种当角色进入触发器时,道具(盒子)从天而降的下雨效果。

最终结果

Server/Index.lua
-- 生成一个触发器
my_trigger = Trigger(Vector(200, 200, 0), Rotator(), 200)

-- 全局定义 my_timer,用于存储计时器
my_timer = nil

-- 设置 BeginOverlap 事件
my_trigger:Subscribe("BeginOverlap", function(trigger, actor_triggering)
-- 仅在角色进入时激活
if (actor_triggering:GetClass() ~= Character) then
return
end

-- 设置一个每 100 毫秒执行一次的计时器来生成道具
my_timer = Timer.SetInterval(function()
-- 获取随机的位置和旋转
local prop_spawn_location = Vector(math.random(100, 300), math.random(100, 300), math.random(800, 1200))
local prop_spawn_rotation = Rotator(math.random(0, 360), math.random(0, 360), math.random(0, 360))

-- 生成一个板条箱
Prop(prop_spawn_location, prop_spawn_rotation, "nanos-world::SM_Crate_07")
end, 100)
end)

-- 设置 EndOverlap 事件
my_trigger:Subscribe("EndOverlap", function(trigger, actor_triggering)
-- 仅在角色离开时激活
if (actor_triggering:GetClass() ~= Character) then
return
end

-- 清除计时器以停止生成道具
if (my_timer ~= nil) then
Timer.ClearInterval(my_timer)
end
end)