cocos creator 3.8 在不同设备上射线检测的结果不同,在设计分辨率下的手机射线检测的结果是正常的,有大佬知道原因及解决方案?
代码:
预格式化文本将缩进 4 格
onTouchStart(event: EventTouch) {
if (!this.camera) return;
this.tapNode = null;
this._dragging = true;
const p = event.getUILocation();
// const p = this.node.getComponent(UITransform).convertToNodeSpaceAR(v3(mousePos.x, mousePos.y, 0));
let ray = new geometry.Ray();
this.camera.screenPointToRay(p.x, p.y, ray);
const origin = ray.o.clone();
let targetOut: Vec3 = new Vec3();
Vec3.normalize(targetOut, ray.d);
Vec3.scaleAndAdd(targetOut, origin, targetOut, 10000000);
gameManager.networkManager.sendMessageToRoom("tapStart", { rayStart: [ray.o.x, ray.o.y, ray.o.z], rayEnd: [targetOut.x, targetOut.y, targetOut.z] });
const mask = 1 << Layers.nameToLayer("toys");
const maxDistance = 10000000;
const queryTigger = true;
if (PhysicsSystem.instance.raycast(ray)) {
const raycastClosetResult = PhysicsSystem.instance.raycastResults;
for (let i = 0, len = raycastClosetResult.length; i < len; i++) {
const result = raycastClosetResult[i];
const colliderItem = raycastClosetResult[i].collider;
if (colliderItem.node.name == "Plane") {
const inp = result.hitPoint;
continue;
};
if (colliderItem.node.name.indexOf('Board') >= 0) continue;
if (colliderItem.node.name.indexOf('CheckBox') >= 0) continue;
Vec3.subtract(this._delta, this.node.worldPosition, result.hitPoint);
this.tapNode = colliderItem.node;
this.tapNode.setWorldPosition(result.hitPoint);
if (gameManager.game.getLockStatuByName(this.tapNode.name)) {
// 节点被锁定
return;
}
const rb = this.tapNode.getComponent(RigidBody);
rb && (rb.useGravity = false);
this.touchStartTime = Date.now();
this.touchEndTime = 0;
// 设置节点锁定状态
gameManager.game.lockNodeByName(this.tapNode.name, true);
}
}
}
屏幕坐标转射线不是应该用屏幕坐标吗? getLocation
getLocation是没有经过适配的坐标,getUILocation是经过适配过的坐标