代码示例
爆炸实例化
local explosion = Instance.new("Explosion")
explosion.BlastRadius = 60
explosion.ExplosionType = Enum.ExplosionType.Craters -- 伤害地形
explosion.Position = Vector3.new(0, 10, 0)
explosion.Parent = workspaceAPI 参考
属性
BlastPressure
代码示例
自定义爆炸
local function customExplosion(position, radius, maxDamage)
local explosion = Instance.new("Explosion")
explosion.BlastPressure = 0 -- 这可以设定得更高,以便对部件施加速度
explosion.DestroyJointRadiusPercent = 0 -- 关节是安全的
explosion.BlastRadius = radius
explosion.Position = position
-- 设置一个表来追踪被击中的模型
local modelsHit = {}
-- 监听接触
explosion.Hit:Connect(function(part, distance)
local parentModel = part.Parent
if parentModel then
-- 检查这个模型是否已经被击中
if modelsHit[parentModel] then
return
end
-- 记录此模型为已击中
modelsHit[parentModel] = true
-- 查找人形
local humanoid = parentModel:FindFirstChild("Humanoid")
if humanoid then
local distanceFactor = distance / explosion.BlastRadius -- 得到一个在 0 到 1 之间的距离值
distanceFactor = 1 - distanceFactor -- 翻转数值,使得较低的值 == 更近 == 更大伤害
humanoid:TakeDamage(maxDamage * distanceFactor) -- TakeDamage 以尊重 ForceFields
end
end
end)
explosion.Parent = game.Workspace
-- Roblox 在几秒钟后移除爆炸,但不摧毁它们。
-- 为确保我们的 .Hit 连接被断开,一旦爆炸被移除,就销毁它。
explosion.AncestryChanged:Connect(function()
if not explosion.Parent then
explosion:Destroy()
end
end)
end
customExplosion(Vector3.new(0, 10, 0), 12, 50)BlastRadius
代码示例
爆炸实例化
local explosion = Instance.new("Explosion")
explosion.BlastRadius = 60
explosion.ExplosionType = Enum.ExplosionType.Craters -- 伤害地形
explosion.Position = Vector3.new(0, 10, 0)
explosion.Parent = workspaceDestroyJointRadiusPercent
代码示例
非致命爆炸
local function onDescendantAdded(instance)
if instance:IsA("Explosion") then
local explosion = instance
explosion.DestroyJointRadiusPercent = 0
local destroyJointRadiusPercent = 1
explosion.Hit:Connect(function(part, distance)
-- 检查该部件是否在破坏关节的范围内
if distance <= destroyJointRadiusPercent * explosion.BlastRadius then
-- 确保该部件不属于角色
if not game.Players:GetPlayerFromCharacter(part.Parent) then
part:BreakJoints()
end
end
end)
end
end
workspace.DescendantAdded:Connect(onDescendantAdded)自定义爆炸
local function customExplosion(position, radius, maxDamage)
local explosion = Instance.new("Explosion")
explosion.BlastPressure = 0 -- 这可以设定得更高,以便对部件施加速度
explosion.DestroyJointRadiusPercent = 0 -- 关节是安全的
explosion.BlastRadius = radius
explosion.Position = position
-- 设置一个表来追踪被击中的模型
local modelsHit = {}
-- 监听接触
explosion.Hit:Connect(function(part, distance)
local parentModel = part.Parent
if parentModel then
-- 检查这个模型是否已经被击中
if modelsHit[parentModel] then
return
end
-- 记录此模型为已击中
modelsHit[parentModel] = true
-- 查找人形
local humanoid = parentModel:FindFirstChild("Humanoid")
if humanoid then
local distanceFactor = distance / explosion.BlastRadius -- 得到一个在 0 到 1 之间的距离值
distanceFactor = 1 - distanceFactor -- 翻转数值,使得较低的值 == 更近 == 更大伤害
humanoid:TakeDamage(maxDamage * distanceFactor) -- TakeDamage 以尊重 ForceFields
end
end
end)
explosion.Parent = game.Workspace
-- Roblox 在几秒钟后移除爆炸,但不摧毁它们。
-- 为确保我们的 .Hit 连接被断开,一旦爆炸被移除,就销毁它。
explosion.AncestryChanged:Connect(function()
if not explosion.Parent then
explosion:Destroy()
end
end)
end
customExplosion(Vector3.new(0, 10, 0), 12, 50)ExplosionType
代码示例
阻止爆炸损坏地形
local function onDescendantAdded(instance)
if instance:IsA("Explosion") then
instance.ExplosionType = Enum.ExplosionType.NoCraters
instance:GetPropertyChangedSignal("ExplosionType"):Connect(function()
instance.ExplosionType = Enum.ExplosionType.NoCraters
end)
end
end
workspace.DescendantAdded:Connect(onDescendantAdded)Position
代码示例
爆炸实例化
local explosion = Instance.new("Explosion")
explosion.BlastRadius = 60
explosion.ExplosionType = Enum.ExplosionType.Craters -- 伤害地形
explosion.Position = Vector3.new(0, 10, 0)
explosion.Parent = workspaceVisible
代码示例
爆炸自定义视觉效果
local function customExplosion(position)
local explosion = Instance.new("Explosion")
explosion.Position = position
explosion.Visible = false
local attachment = Instance.new("Attachment")
attachment.Position = position
attachment.Parent = workspace.Terrain
local particleEmitter = Instance.new("ParticleEmitter")
particleEmitter.Enabled = false
particleEmitter.Parent = attachment
particleEmitter.Speed = NumberRange.new(5, 30)
particleEmitter.SpreadAngle = Vector2.new(-90, 90)
explosion.Parent = workspace
particleEmitter:Emit(20)
task.delay(5, function()
if attachment then
attachment:Destroy()
end
end)
end
customExplosion(Vector3.new(0, 10, 0))活动
Hit
代码示例
自定义爆炸
local function customExplosion(position, radius, maxDamage)
local explosion = Instance.new("Explosion")
explosion.BlastPressure = 0 -- 这可以设定得更高,以便对部件施加速度
explosion.DestroyJointRadiusPercent = 0 -- 关节是安全的
explosion.BlastRadius = radius
explosion.Position = position
-- 设置一个表来追踪被击中的模型
local modelsHit = {}
-- 监听接触
explosion.Hit:Connect(function(part, distance)
local parentModel = part.Parent
if parentModel then
-- 检查这个模型是否已经被击中
if modelsHit[parentModel] then
return
end
-- 记录此模型为已击中
modelsHit[parentModel] = true
-- 查找人形
local humanoid = parentModel:FindFirstChild("Humanoid")
if humanoid then
local distanceFactor = distance / explosion.BlastRadius -- 得到一个在 0 到 1 之间的距离值
distanceFactor = 1 - distanceFactor -- 翻转数值,使得较低的值 == 更近 == 更大伤害
humanoid:TakeDamage(maxDamage * distanceFactor) -- TakeDamage 以尊重 ForceFields
end
end
end)
explosion.Parent = game.Workspace
-- Roblox 在几秒钟后移除爆炸,但不摧毁它们。
-- 为确保我们的 .Hit 连接被断开,一旦爆炸被移除,就销毁它。
explosion.AncestryChanged:Connect(function()
if not explosion.Parent then
explosion:Destroy()
end
end)
end
customExplosion(Vector3.new(0, 10, 0), 12, 50)