Explosion

显示已弃用

*此内容使用人工智能(Beta)翻译,可能包含错误。若要查看英文页面,请点按 此处

爆炸对 BaseParts 内的爆炸 BlastRadius 施加力。这种力打破了 和 部分之间的距离,并击败了不受 保护的角色。Constraints 将不会被爆炸破坏。

如果体验在运行时在数据模型的任何地方被父辈化,那么它立即启动并在几秒内变得无父辈化。在这种情况下,它不会被摧毁 Instance:Destroy() ,因此连接不会断开,父级不会被锁定。与所有实例一样,保持强引用一个 Explosion 将防止其被收集垃圾。

请注意,Explosion必须是Workspace的子孙,爆炸视觉效果才能播放,物理/破坏效果才能产生影响。

爆炸效果

Humanoids 在爆炸中被击败,因为爆炸打破了角色 Model 颈部关节。将 ForceField 作为模型的父辈将保护所有其子女免受爆炸杀伤效果。

如果你不想让 BaseParts 之间的连接被破坏,或者你想实现自己对 Humanoids 造成伤害的方式,建议你将 DestroyJointRadiusPercent 设置为 0 并使用 Hit 事件来处理爆炸的结果。

爆炸也可以配置为对 Terrain 造成伤害,创建火山口,通过 ExplosionType 属性配置。

请注意,爆炸的效果不会被障碍物破坏 ,这意味着被其他部件/地形盾牌后的部件/地形仍然受影响。

代码示例

This code sample includes a brief snippet that creates a large explosion in the game at 0, 10, 0.

Explosion Instantiation

local explosion = Instance.new("Explosion")
explosion.BlastRadius = 60
explosion.ExplosionType = Enum.ExplosionType.Craters -- damages terrain
explosion.Position = Vector3.new(0, 10, 0)
explosion.Parent = workspace

概要

属性

活动

属性

BlastPressure

读取并联

用于确定在 BaseParts 中被捕获的力量的数量 Explosion.BlastRadius

目前,应用的这个力等级不会根据距离从 Explosion.Position 变化。未锚定的 BaseParts 将以相同的速度离开起源地,无论距离有多远,只要它们在爆炸半径内。

爆炸压力决定了爆炸引起零件加速的程度。它不决定关节是否破坏的程度。当 Explosion.DestroyJointRadiusPercent 等于 1 时,所有在 Explosion.BlastRadius 中的零件之间的连接都会被摧毁,只要爆炸压力大于 0。

爆炸压力也不会决定给予 Terrain 的伤害量。提供的爆炸压力大于 0,并且 Explosion.ExplosionType 未设置为 Enum.ExplosionType.NoCraters创建的火山的尺寸仅由 Explosion.BlastRadius 决定。

将爆炸压力设置为 0 可消除爆炸的效果,并在开发人员想要通过 Explosion.Hit 事件编程自定义爆炸行为时有用。

代码示例

This sample contains a simple function that creates a custom explosion. The custom explosion will not break joints as Explosion.DestroyJointRadiusPercent is equal to 0. This means Humanoids will not be instantly killed as their neck joints are broken. In this example explosion.BlastPressure is equal to zero so as not to apply force or damage terrain but this can be changed.

The Explosion.Hit event is used to listen for contact. Once contact has been made the code will look for the parent model of the BasePart hit by the explosion. If that model exists, hasn't already been damaged and has a Humanoid in it damage will be applied based on distance from the explosion's origin.

Custom Explosion

local function customExplosion(position, radius, maxDamage)
local explosion = Instance.new("Explosion")
explosion.BlastPressure = 0 -- this could be set higher to still apply velocity to parts
explosion.DestroyJointRadiusPercent = 0 -- joints are safe
explosion.BlastRadius = radius
explosion.Position = position
-- set up a table to track the models hit
local modelsHit = {}
-- listen for contact
explosion.Hit:Connect(function(part, distance)
local parentModel = part.Parent
if parentModel then
-- check to see if this model has already been hit
if modelsHit[parentModel] then
return
end
-- log this model as hit
modelsHit[parentModel] = true
-- look for a humanoid
local humanoid = parentModel:FindFirstChild("Humanoid")
if humanoid then
local distanceFactor = distance / explosion.BlastRadius -- get the distance as a value between 0 and 1
distanceFactor = 1 - distanceFactor -- flip the amount, so that lower == closer == more damage
humanoid:TakeDamage(maxDamage * distanceFactor) -- TakeDamage to respect ForceFields
end
end
end)
explosion.Parent = game.Workspace
-- Roblox removes explosions after a few seconds, but does not destroy them.
-- To ensure our .Hit connection gets disconnected, destroy the explosion once it's removed.
explosion.AncestryChanged:Connect(function()
if not explosion.Parent then
explosion:Destroy()
end
end)
end
customExplosion(Vector3.new(0, 10, 0), 12, 50)

BlastRadius

读取并联

该属性决定了 Explosion 的半径,以学分为单位。该属性接受 0 到 100 之间的任何值。

该半径决定爆炸的效果区域,而不是爆炸的视觉尺寸。爆炸的视觉效果的大小无论爆炸半径如何(即使爆炸半径为 0)都相同。

BaseParts 在爆炸半径内将受到爆炸影响。意味着,如果 Explosion.BlastPressure 大于 0,力量将被应用到零件。爆炸半径内零件断裂的程度取决于 Explosion.DestroyJointRadiusPercentExplosion.Hit将在半径内发射任何每个BasePart

BaseParts 被视为在 Explosion.BlastRadius 范围内,即使它们只是部分在范围内。

代码示例

This code sample includes a brief snippet that creates a large explosion in the game at 0, 10, 0.

Explosion Instantiation

local explosion = Instance.new("Explosion")
explosion.BlastRadius = 60
explosion.ExplosionType = Enum.ExplosionType.Craters -- damages terrain
explosion.Position = Vector3.new(0, 10, 0)
explosion.Parent = workspace

DestroyJointRadiusPercent

读取并联

用于设置 Explosion.BlastRadius 的比例,在 0 和 1 之间,所有的连接都将被摧毁。任何超出该范围的内容只会受到 Explosion 力的影响。

例如,如果 Explosion.BlastRadius 设置为 100 并且 DestroyJointRadiusPercent 设置为 0.5,那么半径为 50 的任何节点都会被破坏。在 50 和 100 格之间的任何连接点不会被摧毁,但 Explosion 力仍将被应用到 BaseParts

该属性允许开发人员将 Explosions “非致命” 设置为 Humanoids ,通过设置 DestroyJointRadiusPercent 为 0 来实现 “非致命”。这意味着当角色与Explosion接触时,颈部关节不会被破坏。

代码示例

This sample includes an example of how Explosions can be made non lethal to player characters.

Non lethal explosions

local function onDescendantAdded(instance)
if instance:IsA("Explosion") then
local explosion = instance
explosion.DestroyJointRadiusPercent = 0
local destroyJointRadiusPercent = 1
explosion.Hit:Connect(function(part, distance)
-- check the part is in range to break joints
if distance <= destroyJointRadiusPercent * explosion.BlastRadius then
-- make sure the part does not belong to a character
if not game.Players:GetPlayerFromCharacter(part.Parent) then
part:BreakJoints()
end
end
end)
end
end
workspace.DescendantAdded:Connect(onDescendantAdded)

This sample contains a simple function that creates a custom explosion. The custom explosion will not break joints as Explosion.DestroyJointRadiusPercent is equal to 0. This means Humanoids will not be instantly killed as their neck joints are broken. In this example explosion.BlastPressure is equal to zero so as not to apply force or damage terrain but this can be changed.

The Explosion.Hit event is used to listen for contact. Once contact has been made the code will look for the parent model of the BasePart hit by the explosion. If that model exists, hasn't already been damaged and has a Humanoid in it damage will be applied based on distance from the explosion's origin.

Custom Explosion

local function customExplosion(position, radius, maxDamage)
local explosion = Instance.new("Explosion")
explosion.BlastPressure = 0 -- this could be set higher to still apply velocity to parts
explosion.DestroyJointRadiusPercent = 0 -- joints are safe
explosion.BlastRadius = radius
explosion.Position = position
-- set up a table to track the models hit
local modelsHit = {}
-- listen for contact
explosion.Hit:Connect(function(part, distance)
local parentModel = part.Parent
if parentModel then
-- check to see if this model has already been hit
if modelsHit[parentModel] then
return
end
-- log this model as hit
modelsHit[parentModel] = true
-- look for a humanoid
local humanoid = parentModel:FindFirstChild("Humanoid")
if humanoid then
local distanceFactor = distance / explosion.BlastRadius -- get the distance as a value between 0 and 1
distanceFactor = 1 - distanceFactor -- flip the amount, so that lower == closer == more damage
humanoid:TakeDamage(maxDamage * distanceFactor) -- TakeDamage to respect ForceFields
end
end
end)
explosion.Parent = game.Workspace
-- Roblox removes explosions after a few seconds, but does not destroy them.
-- To ensure our .Hit connection gets disconnected, destroy the explosion once it's removed.
explosion.AncestryChanged:Connect(function()
if not explosion.Parent then
explosion:Destroy()
end
end)
end
customExplosion(Vector3.new(0, 10, 0), 12, 50)

ExplosionType

读取并联

这个属性决定了 Explosion 如何与 Terrain 互动。它是一个 Enum.ExplosionType 值,可以设置为三个选项之一。

  • 没有火山口 - 爆炸不会对地形造成伤害
  • 火山口 - 爆炸会在地形中创建火山口
  • 火山和碎片 - 重复,与火山相同的行为

如果爆炸类型设置为在 Terrain 中创建火山口,火山口的半径大约等于 Explosion.BlastRadius 。火山在所有Terrain材料中创建,除了水以外。火山口的大小不受材料影响,虽然一些材料会创建比其他材料更粗糙的边缘。

代码示例

This code sample includes an example of how the Explosion.ExplosionType property can be used to stop Explosions from damaging terrain. It is recommended to set the ExplosionType to NoCraters at the point of Explosion instantiation, but if that is not practical the code below will work.

Stop Explosions from Damaging Terrain

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)

LocalTransparencyModifier

隐藏
未复制
读取并联

Position

读取并联

该属性是 Explosion 中心位置。它在世界空间中定义,不受 Explosion 父元素辈的影响。

BaseParts 将受到爆炸位置的 Explosion 影响,如果它们在 Explosion.BlastRadius 格内。

爆炸的效果是瞬间的。这意味着,尽管爆炸的位置在设置后可以更改,但它不会影响两个不同的区域。一旦爆炸被“引爆”,很快就会将其引爆给 Workspace 的后裔,它就不会再这样做。在一些情况下,爆炸的视觉效果会移动,但没有效果。

因此,如果开发者想要爆炸出现在不同的位置,就必须创建新的爆炸。

代码示例

This code sample includes a brief snippet that creates a large explosion in the game at 0, 10, 0.

Explosion Instantiation

local explosion = Instance.new("Explosion")
explosion.BlastRadius = 60
explosion.ExplosionType = Enum.ExplosionType.Craters -- damages terrain
explosion.Position = Vector3.new(0, 10, 0)
explosion.Parent = workspace

TimeScale

读取并联

在 0 和 1 之间的值,用于控制粒子效果的速度。在 1 处,它以正常速度运行,在 0.5 处,它以半速运行,在 0 处,它冻结时间。

Visible

读取并联

该属性决定是否显示 Explosion 的视觉效果或不显示。

当可见设置为 false 时,爆炸仍会影响 BaseParts 在其 Explosion.BlastRadius 中,唯一的区别是它不会被看到。

为此属性的一次使用将是让开发者使用 ParticleEmitter 制作自己的自定义爆炸效果,同时保留默认的 Explosion 功能。

代码示例

This sample includes a function that will create an Explosion but replace the default Explosion visuals but those of a ParticleEmitter.

Explosion Custom Visuals

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

Explosion 击中其 BasePart 内的某个 Explosion.BlastRadius 时,发生火焰。返回与零件距离相关的部分以及零件与 Explosion.Position 之间的距离。

请注意, 的效果不会受到障碍物的干扰,这意味着被其他部件盾牌的部件仍然会受到攻击,即使它们被锚定。

此事件还会在 Explosion.BlastPressure 等于零时触发。这意味着开发人员可以通过消除爆炸对 BasePartsTerrain 的影响来编程自己的自定义行为,以实现爆炸。

请注意,此事件将在每次 BasePart 命中时触发。这意味着它可以为同一个玩家角色多次发射(因为角色 Model 由多个部分组成)。为此原因,使用 Explosion.Hit 事件处理自定义伤害时,建议实现检查以查看角色是否已被 Explosion 击中。

参数

part: BasePart

BasePart 击中的 Explosion

distance: number

命中距离从 Explosion.Position


代码示例

This sample contains a simple function that creates a custom explosion. The custom explosion will not break joints as Explosion.DestroyJointRadiusPercent is equal to 0. This means Humanoids will not be instantly killed as their neck joints are broken. In this example explosion.BlastPressure is equal to zero so as not to apply force or damage terrain but this can be changed.

The Explosion.Hit event is used to listen for contact. Once contact has been made the code will look for the parent model of the BasePart hit by the explosion. If that model exists, hasn't already been damaged and has a Humanoid in it damage will be applied based on distance from the explosion's origin.

Custom Explosion

local function customExplosion(position, radius, maxDamage)
local explosion = Instance.new("Explosion")
explosion.BlastPressure = 0 -- this could be set higher to still apply velocity to parts
explosion.DestroyJointRadiusPercent = 0 -- joints are safe
explosion.BlastRadius = radius
explosion.Position = position
-- set up a table to track the models hit
local modelsHit = {}
-- listen for contact
explosion.Hit:Connect(function(part, distance)
local parentModel = part.Parent
if parentModel then
-- check to see if this model has already been hit
if modelsHit[parentModel] then
return
end
-- log this model as hit
modelsHit[parentModel] = true
-- look for a humanoid
local humanoid = parentModel:FindFirstChild("Humanoid")
if humanoid then
local distanceFactor = distance / explosion.BlastRadius -- get the distance as a value between 0 and 1
distanceFactor = 1 - distanceFactor -- flip the amount, so that lower == closer == more damage
humanoid:TakeDamage(maxDamage * distanceFactor) -- TakeDamage to respect ForceFields
end
end
end)
explosion.Parent = game.Workspace
-- Roblox removes explosions after a few seconds, but does not destroy them.
-- To ensure our .Hit connection gets disconnected, destroy the explosion once it's removed.
explosion.AncestryChanged:Connect(function()
if not explosion.Parent then
explosion:Destroy()
end
end)
end
customExplosion(Vector3.new(0, 10, 0), 12, 50)