A 力場 保護一個 Humanoid 免受通過 Humanoid:TakeDamage() 方法造成的傷害和保護一個 BaseParts 免受由 Explosion 造成的關節斷裂。當角色在 力場 上重生時,創建了一個新的 SpawnLocation,當 SpawnLocation.Duration 大於零時。
傷害和聯節
A 力場 會影響它所屬的實例。當作為父親到 Model 時,它會保護所有從那個模型下降的 BaseParts 。如果指向 BasePart ,零件的聯接點只會受到保護,如果零件和連接到它的零件都包含 力場 。
力場 只保護Humanoids免受由Humanoid:TakeDamage()造成的傷害。人形仍然可以通過直接設置 Humanoid.Health 來受到傷害。為此原因,建議您使用 Humanoid:TakeDamage() 來分配傷害,同時考慮力場保護。
視覺化
當 ForceField.Visible 設為真實時,粒子效果會被創建。一系列規則決定此效果將從哪裡發射:
- 當父輩到一個 Model 時,如果模型包含一個 Humanoid 名為 Humanoid 的 Humanoid.RigType 設為 R15,效果將從名為 UpperTorso 的部分發出。否則,效果將從名為 軀體 的部分發出。
- 當作為父處理到 BasePart 時,效果將從零件的 BasePart.Position 發出。
範例程式碼
This code sample includes a function that will give a Player a ForceField for a specific duration.
ForceField Instantiation
local Players = game:GetService("Players")
local FORCE_FIELD_DURATION = 15
local function giveForcefield(player, duration)
local character = player.Character
if character then
local forceField = Instance.new("ForceField")
forceField.Visible = true
forceField.Parent = character
if duration then
task.delay(duration, function()
if forceField then
forceField:Destroy()
end
end)
end
end
end
local function onPlayerAdded(player)
player.CharacterAdded:Connect(function(_character)
giveForcefield(player, FORCE_FIELD_DURATION)
end)
end
Players.PlayerAdded(onPlayerAdded)
概要
屬性
Visible
決定是否可以看到 ForceField 粒子效果。將此設為 false 可讓您以下列代碼示例所示的自訂效果取代預設粒子效果。
範例程式碼
這個範例包含一個會替換預設力場粒子效果的功能,使用可由開發人員修改的粒子發射器來進行效果。
自訂力場效果
local Players = game:GetService("Players")
local FORCE_FIELD_DURATION = 15
local function createCustomForcefield(player, duration)
local character = player.Character
if character then
local humanoid = character:FindFirstChild("Humanoid")
if humanoid then
-- 找到軀幹
local torsoName = humanoid.RigType == Enum.HumanoidRigType.R15 and "UpperTorso" or "Torso"
local torso = character:FindFirstChild(torsoName)
if torso then
-- 創建力場
local forceField = Instance.new("ForceField")
forceField.Visible = false -- 不可見
-- 創建粒子效果
local particleEmitter = Instance.new("ParticleEmitter")
particleEmitter.Enabled = true
particleEmitter.Parent = torso
-- 聆聽力場被移除
forceField.AncestryChanged:Connect(function(_child, parent)
if not parent then
if particleEmitter and particleEmitter.Parent then
particleEmitter:Destroy()
end
end
end)
-- 父處力場並將其設為過期
forceField.Parent = character
if duration then
task.delay(duration, function()
if forceField then
forceField:Destroy()
end
end)
end
end
end
end
end
local function onPlayerAdded(player)
player.CharacterAdded:Connect(function(_character)
createCustomForcefield(player, FORCE_FIELD_DURATION)
end)
end
Players.PlayerAdded(onPlayerAdded)