跳至正文
版本:最新版 - a1.169.2 ⚖️

道具雨

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

最终结果​

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)
-- 仅当角色进入时激活(IsA 也匹配继承自角色的类)
if (not actor_triggering:IsA(Character)) then
return
end

-- 同一时间只能有一场雨,否则每个进入的角色都会启动一个新的计时器
if (my_timer ~= nil) 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))

-- 生成一个木箱
local crate = Prop(prop_spawn_location, prop_spawn_rotation, "nanos-world::SM_Crate_07")

-- 10 秒后销毁它,以免服务端充斥着道具
crate:SetLifeSpan(10)
end, 100)
end)

-- 设置 EndOverlap 事件
my_trigger:Subscribe("EndOverlap", function(trigger, actor_triggering)
-- 仅当角色离开时禁用
if (not actor_triggering:IsA(Character)) then
return
end

-- 停止/清除计时器
if (my_timer ~= nil) then
Timer.ClearInterval(my_timer)
my_timer = nil
end
end)