Skip to content

SpaceUnit ​

概览 ​

字段值
KindUnit · 单位 Unit
Realmcommon

继承关系 ​

继承成员 ​

1 个来源 / 5 属性 / 28 函数 / 6 事件

SpaceUnit 是所有位于三维场景中的单位共同继承的空间基类,把轴心读取与改写、基于自身坐标系的平移、以及空间层级查询这几类能力统一收敛到一处。它通过 GetPivot、PivotTo 与 ApplyPivotDelta 描述对象在场景中的位姿,通过 TranslateBy 完成局部方向上的位移,并用 IsTopLevel 与 FindTopLevel 帮助定位对象在层级中的顶层归属,另外提供 EcaPath 属性记录触发器路径。

适用场景 ​

在场景中创建 PhysicsUnit、RenderUnit、TriggerUnit 等具体子类后,常通过 IsA("SpaceUnit") 确认其具备空间能力,再用 GetPivot 读取当前轴心、用 PivotTo 把单位摆到目标位姿,或用 TranslateBy 沿自身朝向做小幅位移。

使用要点 ​

先借助 World:CreateUnit 创建具体子类实例,或使用其他公开接口返回的单位作为操作对象;接着调用 GetPivot 取得当前轴心,用 PivotTo 整体设置目标位姿,或用 ApplyPivotDelta 在现有位姿上叠加增量,需要沿对象自身轴向挪动时改用 TranslateBy。判断层级关系时先用 IsTopLevel 检查是否为顶层对象,必要时用 FindTopLevel 回溯到所属的顶层单位。

注意事项 ​

SpaceUnit 是不可直接创建的空间基类,应创建 WorldUnit、TriggerUnit 等公开具体子类后使用其继承能力。TranslateBy(delta) 沿对象自身坐标系平移,不是世界坐标系;例如对象旋转后,TranslateBy(Vector3.New(0, 0, 1)) 会沿对象本地 Z 轴移动。轴心读写只使用当前公开的 GetPivot、PivotTo 与 ApplyPivotDelta。

代码示例 ​

用具体空间单位调用 SpaceUnit 能力 ​

lua
-- @runtime client
local world = game:GetService("World")

-- SpaceUnit 是基类,脚本中创建具体子类(例如 TriggerUnit)后使用继承能力
local unit = world:CreateUnit("TriggerUnit", {
    Name = "TaggedTrigger",
    Position = Vector3.New(0, 5, 0),
    Scale = Vector3.New(3, 2, 1),
})

local pivot = unit:GetPivot()
unit:PivotTo(pivot * CFrame.New(0, 0, 2))

属性 (1) ​

Name类型默认值说明
EcaPathString""SpaceUnit 上的字符串字段,用于记录该对象对应的触发器路径。该字段可在运行时代码中读取和写入。

关联类型 ​

函数 (6) ​

GetPivot ​

签名:GetPivot() -> CFrame (轴心点的坐标变换矩阵)

读取单位轴心点的坐标变换矩阵,返回一个 CFrame,用于表示轴心当前的位置与朝向。

返回值 CFrame (轴心点的坐标变换矩阵)

示例代码

读取具体空间单位轴心

lua
-- @runtime client
local world = game:GetService("World")
local unit = world:CreateUnit("TriggerUnit", { Position = Vector3.New(0, 5, 0) })

local pivot = unit:GetPivot()
print("当前轴心:", pivot)

PivotTo ​

签名:PivotTo(cframe: CFrame) -> void

把单位的轴心设置为指定的 CFrame,参数 cframe 直接给出目标位置与朝向。

参数类型说明
cframeCFrame目标轴心点的坐标变换矩阵

返回值 void

示例代码

设置具体空间单位轴心

lua
-- @runtime client
local world = game:GetService("World")
local unit = world:CreateUnit("TriggerUnit", { Position = Vector3.New(0, 5, 0) })

unit:PivotTo(CFrame.New(0, 5, 2))
print("已移动到新轴心")

ApplyPivotDelta ​

签名:ApplyPivotDelta(delta: CFrame) -> void

在单位当前轴心的基础上叠加一个增量变换,参数 delta 为 CFrame 类型,用于描述这次要应用的偏移与旋转。

参数类型说明
deltaCFrame要施加的增量 CFrame 变换

返回值 void

示例代码

对具体空间单位应用轴心增量

lua
-- @runtime client
local world = game:GetService("World")
local unit = world:CreateUnit("TriggerUnit", { Position = Vector3.New(0, 5, 0) })

unit:ApplyPivotDelta(CFrame.New(0, 0, 2))
print("已按当前轴心增量移动")

TranslateBy ​

签名:TranslateBy(delta: Vector3) -> void

按对象自身坐标系中的 delta 向量平移当前 SpaceUnit。

参数类型说明
deltaVector3在对象自身坐标系下的平移向量

返回值 void

示例代码

沿本地坐标移动具体空间单位

lua
-- @runtime client
local world = game:GetService("World")
local unit = world:CreateUnit("TriggerUnit", { Position = Vector3.New(0, 5, 0) })

unit:TranslateBy(Vector3.New(0, 0, 1))
print("已沿自身前方移动")

IsTopLevel ​

签名:IsTopLevel() -> Bool (是否为顶层对象)

判断当前 SpaceUnit 是否位于所属层级结构的最顶层,返回布尔结果。

返回值 Bool (是否为顶层对象)

示例代码

判断具体空间单位是否顶层

lua
-- @runtime client
local world = game:GetService("World")
local unit = world:CreateUnit("TriggerUnit", { Name = "TopLevelTrigger" })

if unit:IsTopLevel() then
    print("该单位位于当前层级顶层")
end

FindTopLevel ​

签名:FindTopLevel() -> SpaceUnit (层级树中最顶层的 SpaceUnit,若自身已是顶层则返回自身)

沿 SpaceUnit 的父子层级向上追溯,返回层级树中最顶层的 SpaceUnit;若自身已处于顶层,则返回自身。

返回值 SpaceUnit (层级树中最顶层的 SpaceUnit,若自身已是顶层则返回自身)

示例代码

查找具体空间单位顶层对象

lua
-- @runtime client
local world = game:GetService("World")
local unit = world:CreateUnit("TriggerUnit", { Name = "TopLevelTrigger" })

local top = unit:FindTopLevel()
if top ~= nil then
    print("顶层对象:", top.Name)
end