- Creator 版本:3.8.6
- 目标平台: 编辑器预览 / chrome
- 编辑器操作系统:win10
- 重现概率: 100%
在动态添加RigidBody2D时无法触发回调
左边蓝色方块是手动提前添加的RigidBody2D正常回调了
右边绿色方块是代码中动态添加的RigidBody2D不能正常回调
中间白色方块通过MouseJoint2D鼠标控制来碰到方块应该触发回调
export class rbtest extends Component {
private rb: RigidBody2D | null = null;
private collider: Collider2D | null = null;
protected onLoad(): void {
this.collider = this.getComponent(Collider2D);
if (this.collider) {
this.collider.sensor = true;
this.collider.on(Contact2DType.BEGIN_CONTACT, this.onBeginContact, this);
console.log("添加了Collider碰撞器")
}
this.rb = this.node.getComponent(RigidBody2D);
if (!this.rb) {
this.rb = this.node.addComponent(RigidBody2D);
this.rb.awakeOnLoad = false;
this.rb.type = ERigidBody2DType.Static;
this.rb.enabledContactListener = true;
this.rb.wakeUp();
}
}
start() {
// this.collider.apply();
this.collider.emit(Contact2DType.BEGIN_CONTACT,this.collider);
PhysicsSystem2D.instance.debugDrawFlags = EPhysics2DDrawFlags.Aabb |
EPhysics2DDrawFlags.Pair |
EPhysics2DDrawFlags.CenterOfMass |
EPhysics2DDrawFlags.Joint |
EPhysics2DDrawFlags.Shape;
}
onBeginContact(selfCollider: Collider2D, otherCollider: Collider2D, contact: IPhysics2DContact | null) {
console.log("触发 self->"+selfCollider?.node.name+" other->"+otherCollider?.node.name);
}
}




