- Creator 版本:3.7
两个物体都加上了碰撞体 BoxCollider2D
,碰撞回调能成功检测到碰撞,但在实际画面中两个物体实体会彼此穿透。我想实现两个物体会相互触碰阻挡。
请问是我哪里出错了,还是我需要自己实现这个实体碰撞?如果需要自己实现,该怎么做?
亦或者实体碰撞只有 Box 2D
系统下加刚体才能实现?(教程中说使用内置2D物理系统更省性能)
源码:
import {_decorator, Component, Node, Input, input, Vec2, PhysicsSystem2D, Collider2D, Contact2DType} from "cc"
const { ccclass, property } = _decorator
@ccclass("PlayerController")
export class PlayerController extends Component {
private _touchPos: Vec2 = new Vec2()
private _nodePos: Vec2 = new Vec2()
protected onLoad(): void {
input.on(Input.EventType.TOUCH_MOVE, this.move, this)
}
protected start(): void {
const collider: Collider2D = this.node.getComponent(Collider2D)
if (collider){
collider.on(Contact2DType.BEGIN_CONTACT, this.onBeginContact, this)
}
}
private move(e): void {
this._nodePos.x = this.node.getPosition().x
this._nodePos.y = this.node.getPosition().y
e.getDelta(this._touchPos)
this.node.setPosition(
this._nodePos.x + this._touchPos.x,
this._nodePos.y + this._touchPos.y
)
}
private onBeginContact(){
console.log("Begin Contact")
}
}