碰撞体添加完过后,我们可以看到它与之对应绑定的刚体组件。
如果物体一开始就有刚体,那么在添加完碰撞体之后,它会自动绑定刚体。自动绑定刚体
没有绑定刚体
属性锁定
通过代码获取碰撞体所绑定的刚体组件
@ccclass('ForceController')
export class ForceController extends Component {
/**
* 刚体
*/
private rigidBody: RigidBody = null!;
/**
* 碰撞体
*/
private collider: Collider = null!;
start() {
// 初始化刚体
this.rigidBody = this.node.getComponent(RigidBody);
// 初始化碰撞体
this.collider = this.node.getComponent(Collider);
// 获取碰撞体所绑定的刚体组件
let attachedRigidBody = this.collider.attachedRigidBody;
console.log(this.rigidBody === attachedRigidBody);
}
update(deltaTime: number) {
}
}
输出结果:
true