此基礎類提供處理 3D 空間查詢和模擬的 API,例如 Workspace 和 WorldModel。
概要
屬性
屬性 繼承自 Model設定模型對於啟用時間延遲的體驗的細節程度。
控制 Models 控制器時的模型傳輸行為,當啟用了實例傳輸。
Class.Model 或 nil ,如果未設定。
僅限編輯器的屬性,可以在模型的中心點上調整尺寸。設定此屬性會使尺寸在 Model/ScaleTo 上呼叫。
確定 Model 的中心位置,其中 不 有設置 Model.PrimaryPart 。
方法
如果 BasePart 的任何部分碰觸到其他部分,返回 true。
在指定方向中投擲方塊,並且在 RaycastResult 或 BasePart 中擊中方塊。
返回一個由 定義箱 包圍的列表,其中 盒子 與 方塊子 重疊。
返回一個由 定義 區域重疊的零件群。
返回一個由指定零件和共用零件的組合。
- IKMoveTo(part : BasePart,target : CFrame,translateStiffness : number,rotateStiffness : number,collisionsMode : Enum.IKCollisionsMode):void
將指定的零件移動到指定的位置,而不是直接移動它,以確保任何關聯、限制或碰撞零件參與的剩餘物理上滿意。
使用原始、方向和可選的 RaycastParams 來發射一個光束,然後將返回一個 RaycastResult 如果資格的對象或地形擋住光束。
- Spherecast(position : Vector3,radius : number,direction : Vector3,params : RaycastParams):RaycastResult?
在指定方向中投擲球體形狀,並且在 RaycastResult 或 BasePart 中擊中 Terrain 如果形狀擊中 2>Class.BasePart2> 或 5>Class.Terrain5> 元素,則會返回 8>Datatype.RaycastResult8>。
基於指定的時間增量和可選的 BaseParts 集合來模擬世界中的零件。
將此模型設定為持續為指定玩家。 Model.ModelStreamingMode 必須設置為 PersistentPerPlayer 才能因為添加而變更行為。
返回包含模型所有部分的音量的說明。
返回 BaseParts 的尺寸,Model 與 Model.PrimaryPart 如果設定。
返回此模型對象的所有 Player 對象。 行為因 Script 或 LocalScript 而來自。
返回模型的標準尺寸,其預設為 1 對於新建的模型,並且會在通過 Model/ScaleTo 變更時更改。
將 PrimaryPart 移動到指定位置。如果沒有指定主要零件,則會使用模型的根部分。
此模型不再對指定玩家持續,必須設置為 Model.ModelStreamingMode 才能變更行為,作為結果的一部分。
設定模型的比例因素,以調整所有子孫實例的大小和位置,使其在比例因素變更 1 時,對眾生實例的大小和位置進行相對的比例調整。
將 Model 由所提供的 Vector3 偏移值,保留模型的方向。如果另一個 BasePart 或 1> Class.Terrain1> 已在新位置,則 4> Class.Model4> 將覆蓋所述對物件。
取得 PVInstance 的中心點。
以下是 PVInstance 和所有其子 PVInstances 的描述,以便 pivot 現在位於指定的 CFrame 。
屬性
方法
ArePartsTouchingOthers
ArePartsTouchingOthers 返回 true 如果至少一個 BasePart 正在接觸其他部分。兩個部分被視為 "觸摸" 如果它們位於距離門限內,overlapIgnored 。
如果沒有提供零件,則將返回 false。
參數
一個清單的零件檢查,以確認零件是否碰觸零件不在列表中。
在零件被視為接觸時,零件間的相關性門限將被忽略。
返回
範例程式碼
The code block below demonstrates how to use WorldRoot:ArePartsTouchingOthers() to check if parts in a list are touching any parts in the workspace not in the list.
First the script creates two square parts that overlap 1 stud, Part1 and Part2. Then, it prints the value returned by ArePartsTouchingOthers() when Part1 is passed in partList at three different overlap values: 0, 0.999, and 1. The first two times ArePartsTouchingOthers() is called return false because the overlap values are less than the distance that Part1 and Part2 overlap. The third call returns true because the overlap value is equal to the distance that the parts overlap.
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)) -- True
print(workspace:ArePartsTouchingOthers(partList, 0.999)) -- True
print(workspace:ArePartsTouchingOthers(partList, 1)) -- False
Blockcast
以指定方向拋出方塊形狀,並且在第一次碰撞時以 BasePart 或 Terrain 來返回。這與 WorldRoot:Raycast() 以直線方形拋出碰撞時的方式相同,但它使用 3D 形式而不是方形。
與 WorldRoot:GetPartsInPart() 不同,此方法不會偵測到 BaseParts 那個 最初 交叉形狀。
如果偵測到擊中,RaycastResult 會包含擊中資訊。Distance 代表形狀必須旅行的距離,Position 代表交叉點,會導致擊中。
此方法會在傳送無效的 CFrame、大小或方向輸入時發生錯誤。
參數
初始位置和旋轉方向的 cast 方塊形狀。
螺柱體方塊的尺寸。最大尺寸為 512 螺柱。
方向 shapecast,用代表形狀可以移動的最大距離。最大距離是 1024 格。
返回
範例程式碼
Casts a block and returns the first collision with a BasePart or Terrain. Prints the properties of the RaycastResult if a result was hit.
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
注意,您應該只有使用此功能,如果您確定零件移動是您代碼中的瓶頸。將單個零件和焊接模型的 CFrame 屬性設置為快速即可,在大多數情況下是足夠快的。
參數
返回
GetPartBoundsInBox
WorldRoot:GetPartBoundsInBox() 返回一個用於描述盒子的音量的中心值 ( Datatype.CFrame) 和尺寸 ( CFrame 的盒子。
作為強調,此空間查詢方法能有效考慮零件的限定方塊箱的音量,而不是其實際音量。這可能是對圓柱、球體、聯合和 MeshParts (這些零件的形狀非方塊)的重要考量。對於需要準確度的情況,請使用 Class.WorldRoot:Get
此方法使用 OverlapParams 對象來描述空間查詢的可重用部分,例如包含或排除列表、最大部分數量、要查詢的零件、是否使用 衝突群組 以及是否使用 BasePart.CanCollide 的值。
參數
查詢指定音箱音量中心位置。
要查詢的指定盒子的大小。
包含空間查詢參數的可重用部分。
返回
Class.BasePart|BaseParts 的一個集合,與空間查詢一致。
GetPartBoundsInRadius
WorldRoot:GetPartBoundsInRadius() 返回一個用指定中心 ( Datatype.Vector3 ) 和範圍 (number) 描述的球體,其 Vector3 和 0> 邊界球0> 之間交匯。
作為強調,此空間查詢方法能有效考慮零件的限定方塊箱的音量,而不是其實際音量。這可能是對圓柱、球體、聯合和 MeshParts (這些零件的形狀非方塊)的重要考量。對於需要準確度的情況,請使用 Class.WorldRoot:Get
此方法使用 OverlapParams 對象來描述空間查詢的可重用部分,例如包含或排除列表、最大部分數量、要查詢的零件、是否使用 衝突群組 以及是否使用 BasePart.CanCollide 的值。
參數
查詢指定球體音量中心位置。
可以擷取的球體音量範圍。
包含空間查詢參數的可重用部分。
返回
Class.BasePart|BaseParts 的一個集合,與空間查詢一致。
GetPartsInPart
WorldRoot:GetPartsInPart() 返回一個用於分配給指定零件的方塊,其中的空間與指定零件共用 (必須存在在同一 WorldRoot 下方)。此方法可以用於 BasePart:GetTouchingParts() 的替代方式,通常是更好的選擇。
如所述,此空間查詢方法考慮 準確的音量 ,使用完整的幾何碰撞檢查來匹配指定的零件。為了舉例,凹陷/空心零件不會在它們實際上交錯/碰撞到此零件,除非它們真的交錯/����
此方法使用 OverlapParams 對象來描述空間查詢的可重用部分,例如包含或排除列表、最大部分數量、要查詢的零件、是否使用 衝突群組 以及是否使用 BasePart.CanCollide 的值。
參數
音量與其他音量之間的關係。
包含空間查詢參數的可重用部分。
返回
Class.BasePart|BaseParts 的一個集合,與空間查詢一致。
IKMoveTo
此功能將指定的零件移動到指定位置,而不是直接移動到此,以確保任何關節,Class.Plugin|constraints ,或參與其中的衝突。目前此功能僅在 Studio 可用constraints ,因為它目前與執行中的遊戲的物理學相沖突。
翻譯屈服度 是一個數字,在 0 和 1 之間,指定要匹配零件位置到目標 CFrame 的位置部分。 旋轉屈服度 是一個數字,在 0 與 1 之間,指定要匹配零件旋轉到旋轉部分。
例如:
- 如果彈性和彈性相同,即使是 1 也會移動零件到目標 CFrame 上,無論有何物理限制。
- 如果彈性和旋轉彈性都等同於 0.5,則零件會嘗試移動到正確的 CFrame,但可能會因為物理限制而被推出。
- 如果翻譯硬度和旋轉硬度都等同於 0,則目標 CFrame 將被忽略,物理限制將解決在位置為它所在位置的對象。
參數
正在移動零件。
移動指定零件的位置。
讓您可以指定物理解析度對哪些物體產生效果。
返回
Raycast
使用原始、方向和可選的 RaycastParams 來發射一個光束。如果找到資格的 BasePart 或 Terrain 細胞,發射結果包含運
注意,方向向量的長度(大小)很重要,因為遠離它的長度的對象/地形將不會被測試。如果您使用 CFrame 來協助創建射線零件,請考慮使用 CFrame.LookVector 作為方向向量並乘以所需長度
此方法不使用 Ray 物件,但它的原始和方向零件可以從 Ray.Origin 和 1> Datatype.Ray.Direction1> 借鑑。
參數
射線的起始點。
射線的方向矢量。注意方向矢量的長度會影響測試,因為零件/地形越遠,就越不會被測試。
用於指定射線投射操作中的命中資格。如果未提供,則會使用預設值,其中所有零件都被視為命中,而 Terrain 水不被忽略。
返回
範例程式碼
Casts a ray and returns the first collision with a BasePart or Terrain. Prints the properties of the RaycastResult if a result was hit.
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
參數
返回
Spherecast
以指定方向中的球形形狀投射在指定區域,並且在第一次碰撞時以 BasePart 或 Terrain 來返回。這與 WorldRoot:Raycast() 以 3D 形式投射線形射線相同,但它使用 2D 形式而不是 3D 形。
與 WorldRoot:GetPartsInPart() 不同,此方法不會偵測到 BaseParts 那個 最初 交叉形狀。
如果偵測到擊中,RaycastResult 會包含擊中資訊。Distance 代表形狀必須旅行的距離,Position 代表交叉點,會導致擊中。
此方法會在傳入無效範圍或方向輸入時發生錯誤。
參數
初始位置的投射球體形狀。
零件的球面形狀的範圍。最大範圍是 256 個。
方向 shapecast,用代表形狀可以移動的最大距離。最大距離是 1024 格。
返回
範例程式碼
Casts a sphere and returns the first collision with a BasePart or Terrain. Prints the properties of the RaycastResult if a result was hit.
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 的實例;任何其他類型將被忽略。
返回
範例程式碼
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.
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)