Cocos creator 3.x 刚体组件碰撞无效
问题描述:只有一个circleCollider2D时,可以在碰撞时正确输出结果,但是当我在外围加了一个circle之后,期望character进入圆圈范围时就触发方法,此时原代码失效
import { _decorator, Collider2D, Component, Contact2DType, IPhysics2DContact, Node } from 'cc';
const { ccclass, property } = _decorator;
@ccclass('BombControl')
export class BombControl extends Component {
start() {
let collider = this.getComponent(Collider2D);
if (collider) {
collider.on(Contact2DType.BEGIN_CONTACT, this.onBeginContact, this);
}
}
update(deltaTime: number) {
}
removeNode() {
console.log('remove')
this.node.destroy();
}
onBeginContact (selfCollider: Collider2D, otherCollider: Collider2D, contact: IPhysics2DContact | null) {
// 只在两个碰撞体开始接触时被调用一次
console.log('onBeginContact');
}
}
先上我的解决方案:使用CircleCollider2D
import { _decorator, CircleCollider2D, Collider2D, Component, Contact2DType, IPhysics2DContact, Node } from 'cc';
const { ccclass, property } = _decorator;
@ccclass('BombControl')
export class BombControl extends Component {
start() {
let circleCollider = this.getComponent(CircleCollider2D);
if (circleCollider) {
circleCollider.on(Contact2DType.BEGIN_CONTACT, this.onBeginContact, this);
}
}
update(deltaTime: number) {
}
removeNode() {
console.log('remove')
this.node.destroy();
}
onBeginContact (selfCollider: Collider2D, otherCollider: Collider2D, contact: IPhysics2DContact | null) {
// 只在两个碰撞体开始接触时被调用一次
console.log('onBeginContact');
}
}
请先确保 group 里已经设置好了layer,保证层级这里不会出现问题
// 欢迎大佬补充解决方案