主题
Attachment
概览
| 字段 | 值 |
|---|---|
| Kind | Unit · 单位 Unit |
| Realm | common |
继承关系
- Unit(5 属性 / 28 函数 / 6 事件)
- [Attachment](11 属性 / 1 函数)
继承成员
1 个来源 / 5 属性 / 28 函数 / 6 事件
- 来自 Unit(5 属性 / 28 函数 / 6 事件)
- 属性:
AssetId、Name、Desc、Parent、Tags - 函数:
ClearAllChildren、GetChildren、GetDescendants、IsAncestorOf、IsDescendantOf、FindFirstAncestor、FindFirstAncestorWhichIsA、FindFirstAncestorOfClass、FindFirstChild、FindFirstChildWhichIsA、FindFirstChildOfClass、HasChildren、GetChildCount、GetChildAtIndex、SetAttribute、GetAttribute、GetAllProps、Clone、IsA、Destroy、GetPropertyChangedSignal、GetAttributeChangedSignal、FindFromPath、GetFullPath、GetDebugId、HasTag、AddTag、RemoveTag - 事件:
ChildAdded、ChildRemoved、DescendantAdded、DescendantRemoving、AncestryChanged、Destroying
- 属性:
Attachment 是依附在某个单位上的局部坐标锚点,用 Position、Rotation、Orientation、CFrame 描述相对父级的位置与朝向,同时提供 WorldPosition、WorldRotation、WorldCFrame 这些按父级世界变换换算得到的世界空间结果。Axis 与 SecondaryAxis 用于标定主轴与副轴方向,Visible 控制该锚点的可见状态,GetConstraints 可以取回挂在这个锚点上的全部约束。它本质上是一个轻量的定位节点,不参与渲染形状,只承载变换与约束挂接关系。
适用场景
适合为双端约束、特效挂点等需要固定局部位置的场合提供锚点:把两个 Attachment 分别挂到不同单位上,再用它们的局部变换驱动约束或效果;也可以在命中判定中把它当作定位目标,直接读取 WorldPosition 取得实际世界坐标。
使用要点
先通过 World:CreateUnit("Attachment", config) 或 game:CreateUnit("Attachment", config) 创建实例,并把 Parent 指向目标单位,让锚点跟随该单位运动;随后写入 Position、Rotation、Orientation 或 CFrame 设定局部变换,需要实际坐标时读取 WorldPosition 与 WorldCFrame。若手上只有欧拉角,先用 Quaternion.FromEulerAngles(...) 构造四元数再赋给 Rotation 或 Orientation。要查清该锚点上挂了哪些约束时,调用 GetConstraints 获取约束列表。
注意事项
Attachment 通过 game:CreateUnit("Attachment", config) 创建,不使用 New 构造。Position 是相对 Parent 的局部 Vector3 偏移,Rotation/Orientation 也是局部旋转;需要世界坐标时读取 WorldPosition/WorldCFrame。当前编辑器属性面板可能把 Position 标成“世界坐标”,该标签与 Lua 契约不一致,应以 API 的局部/世界双属性语义为准。ShowOrientation 仅供编辑器属性面板使用(onlyEditor),运行时作者脚本不可读写。若要按欧拉角设置朝向,先用 Quaternion.FromEulerAngles(...) 构造四元数。新建实例若未挂到具体 Unit 上,世界坐标会与原点重合(0,0,0)。GetConstraints 返回当前 Attachment 上挂载的所有约束列表。
代码示例
挂载 Attachment 并对比局部与世界位置
lua
-- @runtime client
local World = game:GetService('World')
local parent = World:CreateUnit('WorldUnit', { Position = Vector3.New(10, 0, 0) })
local attachment = World:CreateUnit('Attachment', {
Parent = parent,
Position = Vector3.New(0, 1, 0),
Rotation = Quaternion.FromEulerAngles(0, math.rad(45), 0),
})
print('局部位置 Position:', attachment.Position)
print('世界位置 WorldPosition:', attachment.WorldPosition)属性 (11)
| Name | 类型 | 默认值 | 说明 |
|---|---|---|---|
Position | Vector3 | [0.0, 0.0, 0.0] | 挂点相对父级的局部位置,类型为 Vector3,默认值为 (0, 0, 0)。 |
Rotation | Quaternion | [0.0, 0.0, 0.0, 1.0] | 局部旋转,类型为 Quaternion,默认值为 (0, 0, 0, 1),即表示无旋转的单位四元数。 |
Orientation | Quaternion | - | 局部朝向,类型为 Quaternion,以四元数形式表示 Attachment 相对父单位坐标系的旋转朝向。 |
CFrame | CFrame | - | 局部 CFrame,类型为 CFrame,用于描述 Attachment 相对其父单位坐标系的位置与朝向。 |
WorldPosition | Vector3 | - | 挂点在世界坐标系下的位置,类型为 Vector3。 |
WorldRotation | Quaternion | - | 世界旋转,类型为 Quaternion,以四元数形式表示 Attachment 在世界坐标系中的旋转朝向。 |
WorldCFrame | CFrame | - | 世界 CFrame,类型为 CFrame,用于描述 Attachment 在世界坐标系中的位置与朝向。 |
Axis | Vector3 | - | 挂点的主轴方向,类型为 Vector3,描述挂点自身朝向中的主方向。 |
SecondaryAxis | Vector3 | - | 挂点的副轴方向,类型为 Vector3,描述挂点自身朝向中的次要参考方向。 |
Visible | Bool | true | 控制 Attachment 在编辑器中是否可见。 |
ShowOrientation | Bool | false | 仅编辑器属性面板使用(onlyEditor)的朝向指示开关;运行时作者脚本不可读写。 |
关联类型
函数 (1)
GetConstraints
签名:GetConstraints() -> Array<Unit> (引用当前 Attachment 的约束单位数组副本。)
获取引用了当前 Attachment 的所有约束单位,返回约束单位数组的副本。
返回值 Array<Unit> (引用当前 Attachment 的约束单位数组副本。)
示例代码
读取锚点引用的约束
lua
-- @runtime client
local World = game:GetService('World')
local attachment = World:FindFirstChild('MyAttachment', true)
if attachment == nil or not attachment:IsA('Attachment') then
print('请先在编辑器中准备已连接约束的 Attachment')
return
end
for _, linkedConstraint in ipairs(attachment:GetConstraints()) do
print('约束名称:', linkedConstraint.Name)
end