3.4.2 2D 碰撞失效

版本:3.4.2

物体信息如下。


脚本如下,却没有事件触发,先感谢回答。有 demo 也行

@ccclass('Player')
export class Player extends Component {
  @property(CCFloat)
  private speed: number = 7;
  @property(Node)
  private background: Node;
  private rigidBody: RigidBody2D;

  onLoad() {
    PhysicsSystem2D.instance.debugDrawFlags = EPhysics2DDrawFlags.Aabb |
    EPhysics2DDrawFlags.Pair |
    EPhysics2DDrawFlags.CenterOfMass |
    EPhysics2DDrawFlags.Joint |
    EPhysics2DDrawFlags.Shape;

    input.on(Input.EventType.TOUCH_START, this.fire.bind(this), this.background);
    this.rigidBody = this.getComponent(RigidBody2D);

    console.log('Loading...');
  }

  start() {
    const collider = this.getComponent(Collider2D);
    collider.on(Contact2DType.BEGIN_CONTACT, this.onBeginContact, this);
    collider.on(Contact2DType.END_CONTACT, this.onEndContact, this);
    collider.on(Contact2DType.PRE_SOLVE, this.onPreSolve, this);
    collider.on(Contact2DType.POST_SOLVE, this.onPostSolve, this);

    // 注册全局碰撞回调函数
    if (PhysicsSystem2D.instance) {
      PhysicsSystem2D.instance.on(Contact2DType.BEGIN_CONTACT, this.onBeginContact, this);
      PhysicsSystem2D.instance.on(Contact2DType.END_CONTACT, this.onEndContact, this);
      PhysicsSystem2D.instance.on(Contact2DType.PRE_SOLVE, this.onPreSolve, this);
      PhysicsSystem2D.instance.on(Contact2DType.POST_SOLVE, this.onPostSolve, this);
    }
  }

  onBeginContact(selfCollider: Collider2D, otherCollider: Collider2D, contact: IPhysics2DContact | null) {
    // will be called once when two colliders begin to contact
    console.log('onBeginContact');
  }
  onEndContact(selfCollider: Collider2D, otherCollider: Collider2D, contact: IPhysics2DContact | null) {
    // will be called once when the contact between two colliders just about to end.
    console.log('onEndContact');
  }
  onPreSolve(selfCollider: Collider2D, otherCollider: Collider2D, contact: IPhysics2DContact | null) {
    // will be called every time collider contact should be resolved
    console.log('onPreSolve');
  }
  onPostSolve(selfCollider: Collider2D, otherCollider: Collider2D, contact: IPhysics2DContact | null) {
    // will be called every time collider contact should be resolved
    console.log('onPostSolve');
  }

  fire(event: EventTouch) {
    const targetLocation = event.getUILocation();
    const position = this.node.position;
    const forceDir = targetLocation.subtract(new Vec2(position.x, position.y)).normalize();
    this.rigidBody.linearVelocity = forceDir.multiplyScalar(this.speed);
  }

  onDestroy() {
    input.off(Input.EventType.TOUCH_START, this.fire, this.background);
  }

  onCollisionEnter(other: any, self: any) {
    console.log('onCollisionEnter');
  }
}
1赞

刚体勾选了enabledContactListener吗

感谢回答,有的。。

奇怪,分组如果设好,勾选了碰撞监听,应该是有碰撞回调的,有碰撞后的表现吗

感谢回复。2d 物体碰撞,得两个物体都需要添加 RigidBody2D,才有事件。。。我记得以前版本2d是有onCollisionEnter 事件的,类似 U3D,只需要 collider 就好了

该主题在最后一个回复创建后14天后自动关闭。不再允许新的回复。