动态添加RigidBody2D触发回调问题

  • 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);
}

}

有没有可能碰撞体被休眠了?Allow Sleep关掉试试


试了下代码里改,编译器里改都没有影响还是没有回调

我知道了因为Static类型碰撞体不能互相碰撞,你场景里不能碰撞的是不是都是Static类型的碰撞体?


应该不是Static类型问题哦,蓝色方块和绿色方块是Static类型白色方块是我用鼠标移动的dynamic类型
且蓝色能正常触发回调,而绿色不行,控制台里也可以看到我测试的一些数据

那我也不知道了