赞
踩
问题描述:只有一个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'); } }
// 欢迎大佬补充解决方案
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。