PhysicsService

显示已弃用

*此内容使用人工智能(Beta)翻译,可能包含错误。若要查看英文页面,请点按 此处

无法创建
服务

PhysicsService 主要包含用于处理 碰撞组 的方法,这些方法定义了集合是否可以或不可以与其他碰撞组中的零件碰撞。您可以通过 注册碰撞组,并通过设置这些部件的 属性为冲突群组的 名称 来分配零件。

创建、删除和修改碰撞组之间的碰撞关系仅限于服务器端 Scripts

请参阅碰撞过滤以获取 Studio 和脚本中的使用细节。

概要

方法

属性

方法

CollisionGroupSetCollidable

()

设置两个群组之间的碰撞状态。如果群组之一未注册,该方法将抛出错误,因此建议您在调用此方法之前确认每个群组的注册通过 PhysicsService:IsCollisionGroupRegistered()

参数

name1: string
默认值:""
name2: string
默认值:""
collidable: boolean
默认值:""

返回

()

CollisionGroupsAreCollidable

返回两个指定碰撞组是否会碰撞。该方法还会返回真值,如果任何组未注册,默认碰撞面罩与所有组碰撞。

参数

name1: string
默认值:""
name2: string
默认值:""

返回

GetMaxCollisionGroups

返回引擎支持的最大碰撞组数。目前值为 32。


返回

GetRegisteredCollisionGroups

返回包含所有场景点碰撞组的表,每个值在返回的表中都是一个表,包含两个成员:


<th>类型</th>
<th>描述</th>
</tr>
</thead>
<tbody>
<tr>
<td>面具</td>
<td>整数</td>
<td>碰撞群组的面具;仅供内部使用。</td>
</tr>
<tr>
<td>名称</td>
<td>字符串</td>
<td>碰撞群组的名称。</td>
</tr>
</tbody>
成员

返回

IsCollisionGroupRegistered

检查是否注册了碰撞组。建议您在调用可能抛出错误的注册未注册碰撞组方法之前调用此方法,例如 PhysicsService:CollisionGroupSetCollidable()

参数

name: string
默认值:""

返回

RegisterCollisionGroup

()

注册一个新的碰撞组件与给定的名称。名称不能是 "Default"

请注意,该方法基于工作区中的 BaseParts 数量有轻微的性能提升,因此建议您在编辑时通过 Studio 编辑器 注册所有碰撞组,并尽可能少地调用 UnregisterCollisionGroup()RenameCollisionGroup() 以实现最佳性能。

参数

name: string
默认值:""

返回

()

代码示例

This example demonstrates one basic use of collision groups. It assigns BallPart to "CollisionGroupBall" and DoorPart to "CollisionGroupDoor", then makes the two groups non-collidable using PhysicsService:CollisionGroupSetCollidable().

PhysicsService:RegisterCollisionGroup

local PhysicsService = game:GetService("PhysicsService")
local collisionGroupBall = "CollisionGroupBall"
local collisionGroupDoor = "CollisionGroupDoor"
-- Register collision groups
PhysicsService:RegisterCollisionGroup(collisionGroupBall)
PhysicsService:RegisterCollisionGroup(collisionGroupDoor)
-- Assign parts to collision groups
script.Parent.BallPart.CollisionGroup = collisionGroupBall
script.Parent.DoorPart.CollisionGroup = collisionGroupDoor
-- Set groups as non-collidable with each other and check the result
PhysicsService:CollisionGroupSetCollidable(collisionGroupBall, collisionGroupDoor, false)
print(PhysicsService:CollisionGroupsAreCollidable(collisionGroupBall, collisionGroupDoor)) --> false

RenameCollisionGroup

()

重命名指定的注册碰撞集团,但不会重命名使用该集团的零件的 属性 。该方法的第一个参数是要重命名的组的名称,第二个参数是组的新名称。如果指定的组不存在,该方法将无所作为。新名称的命名规则与如果群组使用 RegisterCollisionGroup() 被创建时相同。

在以下情况下,该方法将在运行时发生错误:

  • 为任何参数提供的名称无效或为空。
  • 方法从客户端调用。

请注意,该方法基于工作区中的 BaseParts 数量有轻微的性能提升,因此建议您在编辑时通过 Studio 编辑器 注册所有碰撞组,并尽可能多地重命名它们。

参数

from: string
默认值:""
to: string
默认值:""

返回

()

UnregisterCollisionGroup

()

为给定名称注册冲突组时,具有以下行为:

  • 如果提供了无效名称,方法将无所作为。
  • 如果保留名称 "Default" 被提供 如果方法从客户端调用,它将抛出错误。
  • 如果在移除碰撞组时,其中的任何部分仍保留相同的碰撞组名称,那些部分仍将保留相同的碰撞组名称。移除组中零件的物理行为未定义,因此建议将移除组中的任何零件移至另一个群组,例如 "Default" 群组。

请注意,该方法基于工作区中的 BaseParts 数量有轻微的性能提升,因此建议您在编辑时通过 Studio 编辑器 注册所有碰撞组,并尽可能少地调用此方法。

参数

name: string
默认值:""

返回

()

活动