Explosion

顯示已棄用項目

*此內容是使用 AI(Beta 測試版)翻譯,可能含有錯誤。若要以英文檢視此頁面,請按一下這裡

爆炸對 BaseParts 內的爆炸 BlastRadius 施加力。這種力打破了JointInstancesWeldConstraints之間的部件和殺死了Humanoid未受到ForceField保護的角色。Constraints 將不會被爆炸破壞。

如果在體驗執行期間任何地方的數據模型中有一個 Explosion 是父級的,它將立即啟動並在幾秒鐘內變得無父級。在這種情況下,Instance:Destroy() 不會被摧毀,因此連線不會斷開,父端不會被鎖定。與所有情況一樣,保持強引用 an 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

平行讀取

用於確定對 BasePartsExplosion.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 格內。

爆炸的效果是立即的。這意味著,雖然爆炸的位置可以在設置後更改,但它不會影響兩個不同的區域。一旦爆炸被「引爆」後,短時間內將不再這樣做。在某些情況下,爆炸的視覺效果會移動,但沒有效果。

因此,如果開發者想要爆炸出現在不同的位置,就必須創建新的爆炸。

範例程式碼

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.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)