WorldRoot

顯示已棄用項目

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

無法建立

此基礎類提供用於處理 3D 空間查詢和模擬的 API,例如 WorkspaceWorldModel

概要

屬性

屬性 繼承自 Model屬性 繼承自 PVInstance

方法

方法 繼承自 Model方法 繼承自 PVInstance
  • 平行寫入

    獲得 PVInstance 的軸心。

  • PivotTo(targetCFrame : CFrame):()

    將 以及所有其子孫 轉換為指定的 位置,使旋轉點現在位於指定的 位置。

屬性

方法

ArePartsTouchingOthers

ArePartsTouchingOthers 返回真如果給定的 BasePart 至少有一個正在觸碰其他零件。如果兩個部分在距離限制內,則會被視為「接觸」overlapIgnored

如果沒有提供零件,將返回 false 。

參數

partList: Instances

一個列表的零件檢查,以查看列表中的任何零件是否碰觸不在列表中的任何零件。

預設值:""
overlapIgnored: number

零件觸碰前忽略的零件重疊閾值,在零件被認為觸碰之前。

預設值:0.000199999995

返回

如果和只有如果任何一個 partspartList 中觸碰任何其他部分(部分不在部分列表中),則為真;如果沒有部分被傳送,則為假。

範例程式碼

下面的代碼塊展示了如何使用 WorldRoot:ArePartsTouchingOthers() 來檢查列表中的零件是否碰觸列表中未列出的零件。

首先,腳本創建了兩個相互重疊 1 個單位的方塊:Part1Part2 。然後,它在三種重疊值 partList 中印出由 ArePartsTouchingOthers() 返回的值:00.9991 。第一次兩次呼叫 ArePartsTouchingOthers() 返回 false 因為重疊值小於 Part1 和 Part2 重疊的距離。第三次呼叫返回 true 因為重疊值與零件重疊的距離相等。

檢查觸碰零件

local part1 = Instance.new("Part")
part1.Name = "Part1"
part1.Anchored = true
part1.Transparency = 0.5
part1.Color = Color3.fromRGB(185, 100, 38)
part1.Size = Vector3.new(2, 2, 2)
part1.Position = Vector3.new(0, 4, 0)
part1.Parent = workspace
local part2 = Instance.new("Part")
part2.Name = "Part2"
part2.Anchored = true
part2.Transparency = 0.5
part2.Color = Color3.fromRGB(200, 10, 0)
part2.Size = Vector3.new(2, 2, 2)
part2.Position = Vector3.new(0, 5, 0)
part2.Parent = workspace
local partList = { part1 }
print(workspace:ArePartsTouchingOthers(partList, 0)) -- 真的
print(workspace:ArePartsTouchingOthers(partList, 0.999)) -- 真的
print(workspace:ArePartsTouchingOthers(partList, 1)) -- False

Blockcast

平行寫入

在給定的方向投射方塊形狀,並返回使用 BasePartTerrain 細胞的第一次碰撞。這與如何 WorldRoot:Raycast() 在一個方向投射線以找到碰撞類似,但使用 3D 形狀而不是射線。

與 不同,此方法不會偵測到 初始上交叉形狀。

如果偵測到命中,將返回 RaycastResult 包含命中信息的。Distance 屬性代表形狀必須前往的距離才能找到一個命中點,而Position屬性代表造成命中點的交點點。

這個方法會在傳送無效的 CFrame 、尺寸或方向輸入時發出錯誤。

參數

cframe: CFrame

投射方塊形狀的初始位置和旋轉。

預設值:""
size: Vector3

投射方塊的形狀尺寸,以厘米為單位。最大尺寸為 512 厘米。

預設值:""
direction: Vector3

形狀投射的方向,其量表示形狀可以沿途經過的最大距離。最大距離為 1024 個單位。

預設值:""
預設值:"RaycastParams{IgnoreWater=false, BruteForceAllSlow=false, RespectCanCollide=false, CollisionGroup=Default, FilterDescendantsInstances={}}"

返回

包含 shapecast 操作的結果,或 nil 如果沒有符合資格的 BasePartTerrain 細胞被擊中。

範例程式碼

Casts a block and returns the first collision with a BasePart or Terrain. Prints the properties of the RaycastResult if a result was hit.

Blockcasting

local Workspace = game:GetService("Workspace")
local function castBlock()
-- The initial position and rotation of the cast block shape
local originCFrame = CFrame.new(Vector3.new(0, 50, 0))
-- The size of the cast block shape
local size = Vector3.new(6, 3, 9)
-- The direction the block is cast in
local direction = -Vector3.yAxis
-- The maximum distance of the cast
local distance = 50
-- Cast the block and create a visualization of it
local raycastResult = Workspace:Blockcast(originCFrame, size, direction * distance)
if raycastResult then
-- Print all properties of the RaycastResult if it exists
print(`Block intersected with: {raycastResult.Instance:GetFullName()}`)
print(`Intersection position: {raycastResult.Position}`)
print(`Distance between block's initial position and result: {raycastResult.Distance}`)
print(`The normal vector of the intersected face: {raycastResult.Normal}`)
print(`Material hit: {raycastResult.Material.Name}`)
else
print("Nothing was hit")
end
end
-- Continually cast a block every 2 seconds
while true do
castBlock()
task.wait(2)
end

BulkMoveTo

()

此功能將 BaseParts 表移動到 CFrames 表,而不一定發射預設屬性 Changed 事件。這樣可以大大加快移動大量零件的速度,因為您不需要為每個零件支付單獨的屬性集成本。

第三個參數允許您進一步優化運動操作。預設情況下,每個零件的 Changed 事件會發射到 Position , OrientationCFrame 。然而,如果您指定 FireCFrameChanged 為第三個引數,只有 Changed 事件對 CFrame 屬性發觸發。

請注意,您只應使用此功能,如果您確定零件移動是您程式碼中的瓶頸。簡單設置個別零件和焊接模型的 CFrame 屬性即可在大多數情況下快速完成。

參數

partList: Instances
預設值:""
cframeList: Array
預設值:""
預設值:"FireAllEvents"

返回

()

GetPartBoundsInBox

Instances
平行寫入

WorldRoot:GetPartBoundsInBox() 返回一個包含零件的數組,其 綁定箱子 覆蓋一個描述使用給定中心 ( CFrame ) 和尺寸 ( Vector3 ) 的盒子的音量的盒子。

如已強調,這個空間查詢方法有效地考慮零件的綁定盒子的體積,而不是它們實際上使用的體積。這可能在考慮圓筒、球、聯合和 MeshParts 非積木形狀時很重要。對於需要精確度特別重要的情況,請使用 WorldRoot:GetPartsInPart() 或自行過濾此方法的結果。

此方法使用 對象來描述可重複的空間查詢部分,例如包括或排除列表、最大要查詢的零件數量、要使用的 碰撞群 以及是否將查詢值優先於其值的交叉部分的 值。

參數

cframe: CFrame

要查詢的盒子卷中心的位置。

預設值:""
size: Vector3

要查詢的盒子體積的大小。

預設值:""
overlapParams: OverlapParams

包含可重複使用的空間查詢參數部分。

預設值:"OverlapParams{MaxParts=0, Tolerance=0, BruteForceAllSlow=false, RespectCanCollide=false, CollisionGroup=Default, FilterDescendantsInstances={}}"

返回

Instances

一個匹配空間查詢的 BaseParts 陣列。

GetPartBoundsInRadius

Instances
平行寫入

WorldRoot:GetPartBoundsInRadius() 返回一個包含零件的數組,其 綁定箱子 覆蓋一個屬於給定中心 ( Vector3 ) 和半徑 (number) 的球體的音量。

如已強調,這個空間查詢方法有效地考慮零件的綁定盒子的體積,而不是它們實際上使用的體積。這可能在考慮圓筒、球、聯合和 MeshParts 非積木形狀時很重要。對於需要精確度特別重要的情況,請使用 WorldRoot:GetPartsInPart() 或自行過濾此方法的結果。

此方法使用 對象來描述可重複的空間查詢部分,例如包括或排除列表、最大要查詢的零件數量、要使用的 碰撞群 以及是否將查詢值優先於其值的交叉部分的 值。

參數

position: Vector3

要查詢的給定球體體積中心的位置。

預設值:""
radius: number

要查詢的給定球體體積範圍。

預設值:""
overlapParams: OverlapParams

包含可重複使用的空間查詢參數部分。

預設值:"OverlapParams{MaxParts=0, Tolerance=0, BruteForceAllSlow=false, RespectCanCollide=false, CollisionGroup=Default, FilterDescendantsInstances={}}"

返回

Instances

一個匹配空間查詢的 BaseParts 陣列。

GetPartsInPart

Instances
平行寫入

WorldRoot:GetPartsInPart() 返回一個包含與給定部分共享空間的零件數組(必須存在於與要查詢的零件相同的 WorldRoot 中)。這個方法可以在 BasePart:GetTouchingParts() 的位置使用,通常是更好的選擇。

如已注意,這個空間查詢方法會考慮使用完整的幾何碰撞檢查來確定指定零件所佔的 準確體積 。舉例來說,凹/空心零件不會與內部的查詢零件匹配,除非它們實際上覆蓋/觸碰這樣的零件。對於簡單的音量,請考慮使用 WorldRoot:GetPartBoundsInBox()WorldRoot:GetPartBoundsInRadius(),因為它們的準確度較低,但執行效率較高。

此方法使用 對象來描述可重複的空間查詢部分,例如包括或排除列表、最大要查詢的零件數量、要使用的 碰撞群 以及是否將查詢值優先於其值的交叉部分的 值。

參數

part: BasePart

音量要與其他零件進行比較的零件。

預設值:""
overlapParams: OverlapParams

包含可重複使用的空間查詢參數部分。

預設值:"OverlapParams{MaxParts=0, Tolerance=0, BruteForceAllSlow=false, RespectCanCollide=false, CollisionGroup=Default, FilterDescendantsInstances={}}"

返回

Instances

一個匹配空間查詢的 BaseParts 陣列。

IKMoveTo

()
外掛程式安全性

這個功能會將指定的零件移動到指定位置,通過 逆動力學 而不是直接移動到那裡,以確保參與其中的任何關節、constraints 或碰撞都能物理上滿足。目前此功能只能在 Studio 到 plugins 中使用,因為目前與運行中的遊戲的物理發生衝突。

翻譯剛度 是一個數字,介於 0 和 1 之間,指定要如何積極地將零件的位置匹配到目標 CFrame 的位置。 旋轉剛度 是一個數字,介於 0 和 1 之間,指定要如何積極地將零件的旋轉匹配到目標 CFrame 的旋轉部分。

例如:

  • 如果翻譯剛度和旋轉剛度兩者都等於 1,則零件將準確移動到目標 CFrame,無論其上有何物理限制。
  • 如果翻譯剛度和旋轉剛度兩者都等於 0.5,則零件將嘗試移動到準確的目標 CFrame,但可能會被物理限制推出路徑。
  • 如果翻譯剛度和旋轉剛度兩者都等於 0,則目標 CFrame 將被忽略,物理限制將在位置上解決對象所在的位置。

參數

part: BasePart

正在移動的零件。

預設值:""
target: CFrame

移動指定零件的位置。

預設值:""
translateStiffness: number

一個數字介於 0 和 1 之間,指定要如何積極地將零件的位置與目標位置匹配 CFrame

預設值:0.5
rotateStiffness: number

一個數字介於 0 和 1 之間,指定要如何積極地將零件的旋轉匹配到目標的旋轉部分 CFrame

預設值:0.5
collisionsMode: Enum.IKCollisionsMode

讓你指定物件應受物理解析度影響的範圍。

預設值:"OtherMechanismsAnchored"

返回

()
平行寫入

使用起源、方向和可選 RaycastParams 來投射射線。如果找到符合要求的 BasePartTerrain 細胞,將返回包含操作結果的 RaycastResult。如果未提供任何 對象,則使用預設值 (所有零件都被考慮,水不被忽略)。

請注意,方向向量的長度(強度)很重要,因為距離超過其長度的物體/地形不會進行測試。如果您使用 CFrame 來幫助創建射線組件,請考慮使用 CFrame.LookVector 作為方向向量,並將其乘以所需的長度,如下圖所示。方向向量的最大長度為 15,000 個單位。

此方法不使用 使用 Ray 物件,但其起源和方向組件可以從 Ray.OriginRay.Direction 借用。

參數

origin: Vector3

射線的起點。

預設值:""
direction: Vector3

射線的方向向量。注意,這個向量的長度很重要,因為部件/地形比它的長度更遠,將不會進行測試。

預設值:""
raycastParams: RaycastParams

一個用於在射線投射操作中指定命中資格的對象。如果未提供,將在所有零件被考慮的情況下使用預設值,並且不忽略 Terrain 水。

預設值:"RaycastParams{IgnoreWater=false, BruteForceAllSlow=false, RespectCanCollide=false, CollisionGroup=Default, FilterDescendantsInstances={}}"

返回

包含射線投射操作的結果,或 nil 如果沒有符合資格的 BasePartTerrain 細胞被擊中。

範例程式碼

Casts a ray and returns the first collision with a BasePart or Terrain. Prints the properties of the RaycastResult if a result was hit.

Raycasting

local Workspace = game:GetService("Workspace")
local function castRay()
-- The origin point of the ray
local originPosition = Vector3.new(0, 50, 0)
-- The direction the ray is cast in
local direction = -Vector3.yAxis
-- The maximum distance of the ray
local distance = 50
-- Cast the ray and create a visualization of it
local raycastResult = Workspace:Raycast(originPosition, direction * distance)
if raycastResult then
-- Print all properties of the RaycastResult if it exists
print(`Ray intersected with: {raycastResult.Instance:GetFullName()}`)
print(`Intersection position: {raycastResult.Position}`)
print(`Distance between ray origin and result: {raycastResult.Distance}`)
print(`The normal vector of the intersected face: {raycastResult.Normal}`)
print(`Material hit: {raycastResult.Material.Name}`)
else
print("Nothing was hit")
end
end
-- Continually cast a ray every 2 seconds
while true do
castRay()
task.wait(2)
end

Shapecast

參數

part: BasePart
預設值:""
direction: Vector3
預設值:""
預設值:"RaycastParams{IgnoreWater=false, BruteForceAllSlow=false, RespectCanCollide=false, CollisionGroup=Default, FilterDescendantsInstances={}}"

返回

Spherecast

平行寫入

在給定的方向投射球形狀態,並返回使用 BasePartTerrain 細胞的第一次碰撞。這與如何 WorldRoot:Raycast() 在一個方向投射線以找到碰撞類似,但使用 3D 形狀而不是射線。

與 不同,此方法不會偵測到 初始上交叉形狀。

如果偵測到命中,將返回 RaycastResult 包含命中信息的。Distance 屬性代表形狀必須前往的距離才能找到一個命中點,而Position屬性代表造成命中點的交點點。

這個方法會在傳送無效的半徑或方向輸入時發生錯誤。

參數

position: Vector3

投射球形狀態的初始位置。

預設值:""
radius: number

投射圓形形狀的範圍,以厘米為單位。最大範圍為 256 厘米。

預設值:""
direction: Vector3

形狀投射的方向,其量表示形狀可以沿途經過的最大距離。最大距離為 1024 個單位。

預設值:""
預設值:"RaycastParams{IgnoreWater=false, BruteForceAllSlow=false, RespectCanCollide=false, CollisionGroup=Default, FilterDescendantsInstances={}}"

返回

包含 shapecast 操作的結果,或 nil 如果沒有符合資格的 BasePartTerrain 細胞被擊中。

範例程式碼

Casts a sphere and returns the first collision with a BasePart or Terrain. Prints the properties of the RaycastResult if a result was hit.

Spherecasting

local Workspace = game:GetService("Workspace")
local function castSphere()
-- The initial position of the cast spherical shape
local originPosition = Vector3.new(0, 50, 0)
-- The radius of the cast spherical shape in studs
local radius = 10
-- The direction the sphere is cast in
local direction = -Vector3.yAxis
-- The maximum distance of the cast
local distance = 50
-- Cast the sphere and create a visualization of it
local raycastResult = Workspace:Spherecast(originPosition, radius, direction * distance)
if raycastResult then
-- Print all properties of the RaycastResult if it exists
print(`Sphere intersected with: {raycastResult.Instance:GetFullName()}`)
print(`Intersection position: {raycastResult.Position}`)
print(`Distance between sphere's initial position and result: {raycastResult.Distance}`)
print(`The normal vector of the intersected face: {raycastResult.Normal}`)
print(`Material hit: {raycastResult.Material.Name}`)
else
print("Nothing was hit")
end
end
-- Continually cast a sphere every 2 seconds
while true do
castSphere()
task.wait(2)
end

StepPhysics

()
外掛程式安全性

基於指定的時間增量和可選的 BasePart 集合,對世界上的零件進行模擬前進。當指定一組零件時,只有這些零件會被模擬,世界上所有其他零件都會被視為已錨定。當此參數被忽略時,世界上所有的零件都會包含在模擬中。指定的時間增量可以是任何正數,大值會增加函數的運行時間。根據時間增量的值,物理系統可能會將其分解為多個獨立步驟來維持模擬的準確度和穩定性。即使函數執行多個替換步驟,模擬結果也只會在函數完成後才會被看到。要視覺化模擬的個別步驟,該功能可以通過 RunService.RenderStepped 事件每次渲染步驟呼叫一次。

參數

dt: number

模擬的時間數量。此參數必須為正數。較大的值會增加此函數的運行時間。

預設值:""
parts: Instances

可選擇的零件數組,將被模擬。此集必須包含類型 BasePart 的實例;其他類型將被忽略。

預設值:"{}"

返回

()

範例程式碼

Simulates the parts in the workspace for a fixed period of time by calling the StepPhysics function once per frame until a specified time has elaspsed.

StepPhysics

local RunService = game:GetService("RunService")
-- Optional array of parts to simulate; otherwise all parts will be simulated
local partsToSimulate = {
workspace.Part,
}
local function simulateParts(duration)
local time = 0.0
local stepJob
stepJob = RunService.RenderStepped:Connect(function(dt)
if time + dt > duration then
dt = duration - time
end
workspace:StepPhysics(dt, partsToSimulate)
time = time + dt
if time >= duration then
stepJob:Disconnect()
end
end)
end
-- Simulate workspace parts for 5 seconds, stepping the parts once per frame
simulateParts(5.0)

活動