Explosion

Hiển Thị Bản Đã Lỗi Thời

*Nội dung này được dịch bằng AI (Beta) và có thể có lỗi. Để xem trang này bằng tiếng Anh, hãy nhấp vào đây.

Một nổ áp dụng lực vào BaseParts bên trong nổ BlastRadius .Sức lực này phá vỡ JointInstancesWeldConstraints giữa các bộ phận và giết Humanoid nhân vật không được bảo vệ bởi một ForceField .Constraints sẽ không bị phá vỡ bởi một vụ nổ.

Nếu một Explosion là cha của bất cứ nơi nào trong mô hình dữ liệu trong khi trải nghiệm đang chạy, nó ngay lập tức bật lên và, trong vòng vài giây, nó trở nên không cha.Nó không bị phá hủy với Instance:Destroy() trong trường hợp này, vì vậy các kết nối không bị cắt kết nối và cha không bị khóa.Như với tất cả các ví dụ, giữ một tham chiếu mạnh an Explosion sẽ ngăn chặn nó không bị thu thập rác.

Lưu ý rằng một Explosion phải là con cháu của Workspace để các hình ảnh nổ và các hiệu ứng vật lý/gây hại có ảnh hưởng.

Hiệu ứng nổ

Humanoids bị giết bởi những vụ nổ, khi vụ nổ phá vỡ khớp cổ tính nhân Model .Nuôi dưỡng một ForceField đến một mô hình sẽ bảo vệ tất cả con cái của nó khỏi hiệu ứng giết chết do nổ.

Nếu bạn không muốn các khớp giữa và được phá vỡ, hoặc bạn muốn thực hiện công thức riêng của mình đối với việc làm hại , thì được khuyến khích bạn đặt thành 0 và sử dụng sự kiện để xử lý kết quả của vụ nổ.

Các vụ nổ cũng có thể được cấu hình để gây thiệt hại Terrain , tạo ra các hố, như được cấu hình thông qua thuộc tính ExplosionType.

Lưu ý rằng hiệu ứng của một vụ nổ là không bị gián đoạn bởi chướng ngại vật, có nghĩa là các bộ phận/địa hình bị che chắn phía sau các bộ phận/địa hình khác vẫn sẽ bị ảnh hưởng.

Mẫu mã

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

Tóm Tắt

Thuộc Tính

  • Đọc Song Song

    Được sử dụng để xác định lượng lực được áp dụng cho BaseParts bị bắt trong Explosion.BlastRadius .

  • Đọc Song Song

    Tính chất này xác định bán kính của Explosion, bằng đơn vị studs.Bán kính này xác định khu vực ảnh hưởng của vụ nổ, không phải kích thước của hình ảnh vụ nổ.

  • Dùng để đặt tỷ lệ của Explosion.BlastRadius, từ 0 đến 1, trong đó tất cả các khớp sẽ bị phá hủy.Bất cứ thứ gì ngoài phạm vi này chỉ có thể có được lực Explosion áp dụng cho nó.

  • Tính chất này xác định cách Explosion sẽ tương tác với Terrain . Được sử dụng để xác định xem liệu những vụ nổ có gây thiệt hại cho địa hình hay không.

  • Ẩn
    Không Sao Chép
    Đọc Song Song
  • Đọc Song Song

    Thuộc tính này là vị trí của trung tâm của Explosion . Nó được định nghĩa trong không gian thế giới và không bị ảnh hưởng bởi cha mẹ Explosion .

  • Đọc Song Song

    Giá trị nằm giữa 0 và 1 kiểm soát tốc độ của hiệu ứng hạt.

  • Đọc Song Song

    Tính chất này xác định liệu có hiển thị hiệu ứng hình ảnh của một Explosion hay không.

Sự Kiện

Thuộc Tính

BlastPressure

Đọc Song Song

Được sử dụng để xác định lượng lực được áp dụng cho BaseParts bị bắt trong Explosion.BlastRadius .

Hiện tại mức lực áp dụng này không thay đổi dựa trên khoảng cách từ Explosion.Position .Unanchored BaseParts sẽ tăng tốc đồng đều không phụ thuộc vào khoảng cách miễn là chúng nằm trong bán kính nổ.

Áp lực nổ xác định tốc độ gia tốc của các bộ phận do một vụ nổ.Nó không xác định độ mà các khớp bị phá vỡ.Khi Explosion.DestroyJointRadiusPercent bằng 1 tất cả các khớp giữa các bộ phận trong Explosion.BlastRadius sẽ bị phá hủy miễn là Áp lực nổ lớn hơn 0.

Áp lực nổ cũng không xác định số lượng sát thương được đưa cho Terrain .Áp lực nổ được cung cấp lớn hơn 0 và Explosion.ExplosionType không được đặt thành Enum.ExplosionType.NoCraters kích thước của vết nứt được xác định hoàn toàn bởi Explosion.BlastRadius .

Đặt Áp lực nổ lên 0 loại bỏ hiệu ứng của vụ nổ và hữu ích khi các nhà phát triển muốn lập trình hành vi tùy chỉnh riêng của họ cho các vụ nổ bằng sự kiện Explosion.Hit.

Mẫu mã

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

Đọc Song Song

Thuộc tính này xác định bán kính của Explosion , bằng studs. Thuộc tính này chấp nhận bất kỳ giá trị nào giữa 0 và 100.

Bán kính này xác định khu vực ảnh hưởng của Nổ, không phải kích thước của hình ảnh Nổ.Kích thước hiệu ứng hình ảnh của nổ là giống nhau, bất kể BlastRadius (ngay cả khi BlastRadius là 0).

BaseParts bên trong BlastRadius sẽ bị ảnh hưởng bởi vụ nổ.Nghĩa là, nếu Explosion.BlastPressure lớn hơn 0, lực sẽ được áp dụng cho các bộ phận.Độ mà các khớp bị phá vỡ trong Bán kính nổ phụ thuộc vào Explosion.DestroyJointRadiusPercent .Explosion.Hit sẽ bắn cho bất kỳ mỗi BasePart bên trong bán kính.

BaseParts được xem xét trong Explosion.BlastRadius ngay cả khi chúng chỉ ở trong phạm vi một phần.

Mẫu mã

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

Đọc Song Song

Dùng để đặt tỷ lệ của Explosion.BlastRadius, từ 0 đến 1, trong đó tất cả các khớp sẽ bị phá hủy.Bất cứ thứ gì ngoài phạm vi này chỉ có thể có được lực Explosion áp dụng cho nó.

Ví dụ, nếu Explosion.BlastRadius được đặt thành 100 và DestroyJointRadiusPercent được đặt thành 0.5, bất kỳ khớp nào trong bán kính 50 đinh tán sẽ bị phá vỡ.Bất kỳ khớp nối nào giữa phạm vi từ 50 đến 100 đinh tán sẽ không bị phá hủy, nhưng lực Explosion vẫn sẽ được áp dụng cho BaseParts .

Tính chất này cho phép các nhà phát triển làm Explosions 'không chết người' cho Humanoids bằng cách đặt DestroyJointRadiusPercent thành 0.Điều này có nghĩa là khớp cổ sẽ không bị phá vỡ khi các nhân vật tiếp xúc với Explosion .

Mẫu mã

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

Đọc Song Song

Thuộc tính này xác định cách Explosion sẽ tương tác với Terrain. Nó là một giá trị Enum.ExplosionType và có thể được đặt thành một trong ba lựa chọn.

  • Không có Craters - Những vụ nổ sẽ không làm hư hại Địa hình
  • Núi lửa - Những vụ nổ sẽ tạo ra các núi lửa trên địa hình
  • CratersAndDebris - Trùng lặp, hành vi giống như Craters

Nếu ExplosionType được đặt để tạo các vết nứt trong Terrain , bán kính của vết nứt sẽ gần bằng với Explosion.BlastRadius .Các vết nứt được tạo ra trong tất cả các Terrain vật liệu khác ngoài nước.Kích thước của vùng nham thạch không bị ảnh hưởng bởi vật liệu, mặc dù một số vật liệu tạo ra các cạnh thô hơn so với những vật liệu khác.

Mẫu mã

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

Ẩn
Không Sao Chép
Đọc Song Song

Position

Đọc Song Song

Thuộc tính này là vị trí của trung tâm của Explosion . Nó được định nghĩa trong không gian thế giới và không bị ảnh hưởng bởi cha mẹ Explosion .

BaseParts sẽ bị ảnh hưởng bởi Explosion nếu chúng nằm trong Explosion.BlastRadius đinh tán của vị trí nổ.

Hiệu ứng của một vụ nổ là ngay lập tức.Điều này có nghĩa là mặc dù vị trí của một vụ nổ có thể được thay đổi sau khi đã được thiết lập nó không ảnh hưởng đến hai khu vực khác nhau.Một khi một vụ nổ đã được 'kích nổ', ngay sau khi nuôi giống nó cho một con cháu của Workspace, nó sẽ không làm như vậy một lần nữa.Trong một số trường hợp, hiệu ứng hình ảnh của vụ nổ sẽ di chuyển nhưng nó sẽ không có hiệu lực.

Vì lý do này, một nổ mới nên được tạo nếu nhà phát triển muốn một nổ xuất hiện ở vị trí khác.

Mẫu mã

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

Đọc Song Song

Một giá trị nằm giữa 0 và 1 kiểm soát tốc độ của hiệu ứng hạt.Tại 1 nó chạy với tốc độ bình thường, tại 0.5 nó chạy với tốc độ một nửa và tại 0 nó đóng băng thời gian.

Visible

Đọc Song Song

Tính chất này xác định liệu có hiển thị hiệu ứng hình ảnh của một Explosion hay không.

Khi Visible được đặt thành false, vụ nổ vẫn sẽ ảnh hưởng đến BaseParts ở trong Explosion.BlastRadius, sự khác biệt duy nhất là nó sẽ không được nhìn thấy.

Một lần sử dụng cho tài sản này sẽ là cho một nhà phát triển tạo ra các hiệu ứng nổ tùy chỉnh của riêng họ bằng cách sử dụng ParticleEmitter , trong khi giữ nguyên chức năng mặc định Explosion .

Mẫu mã

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

Phương Pháp

Sự Kiện

Hit

Bắt lửa khi Explosion đánh vào BasePart trong phạm vi Explosion.BlastRadius của nó. Trả về phần bị đánh cùng với khoảng cách của phần từ Explosion.Position .

Lưu ý rằng hiệu ứng của một không bị gián đoạn bởi chướng ngại vật, điều này có nghĩa là các bộ phận được che chắn phía sau các bộ phận khác vẫn sẽ bị tấn công, ngay cả khi bộ phận che chắn phía sau được neo.

Sự kiện này cũng sẽ bắt lửa khi Explosion.BlastPressure bằng không.Điều này có nghĩa là các nhà phát triển có thể lập trình hành vi tùy chỉnh của riêng họ cho các vụ nổ bằng cách loại bỏ tác động của vụ nổ lên BasePartsTerrain .

Lưu ý rằng sự kiện này sẽ bắn cho mỗi BasePart lần đánh.Điều này có nghĩa là nó có thể bắn nhiều lần cho cùng một nhân vật người chơi (vì nhân vật Model được tạo bởi nhiều phần).Vì lý do này, khi xử lý sát thương tùy chỉnh bằng sự kiện Explosion.Hit nó được khuyến nghị thực hiện kiểm tra để xem liệu nhân vật đã bị đánh bởi Explosion chưa.

Tham Số

part: BasePart

Cú đánh BasePart bởi Explosion .

distance: number

Khoảng cách của cú đánh từ Explosion.Position.


Mẫu mã

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)