现在想做的是计算3D世界空间坐标下屏幕边界的位置,通过点击事件发送射线检测的结果是正确的,但是通过一个固定的数据检测的时候,检测的结果不正确。下面是代码:
预格式化文本将缩进 4 格`import { _decorator, Camera, Component, EventTouch, geometry, Input, input, Node, PhysicsSystem, view } from ‘cc’;
const { ccclass, property } = _decorator;
@ccclass(‘UIManager’)
export class UIManager extends Component {
@property(Camera)
d3Camera: Camera = null;
start() {
this._checkBoard();
}
private _checkBoard() {
const viewSize = view.getVisibleSize();
let ray = new geometry.Ray();
this.d3Camera.screenPointToRay(viewSize.width,viewSize.height,ray);
const hasRes = PhysicsSystem.instance.raycast(ray);
if(hasRes) {
const res = PhysicsSystem.instance.raycastResults;
res.forEach(item => {
console.log("hitpoint is ",item.hitPoint);
console.log("hitnode name is ",item.collider.node.name);
});
}
}
protected onEnable(): void {
input.on(Input.EventType.TOUCH_START,this.tapStart,this);
}
protected onDisable(): void {
input.off(Input.EventType.TOUCH_START,this.tapStart,this);
}
public tapStart(event: EventTouch){
const pos = event.getUILocation();
console.log("pos is ",pos);
const ray = new geometry.Ray();
this.d3Camera.screenPointToRay(pos.x,pos.y,ray);
const hasRes = PhysicsSystem.instance.raycast(ray);
if(hasRes) {
const res = PhysicsSystem.instance.raycastResults;
res.forEach(item => {
console.log("hitpoint is ",item.hitPoint);
console.log("hitnode name is ",item.collider.node.name);
});
}
}
update(deltaTime: number) {
}
}
代码中是首先调用了检测边界的方法,不是通过touch事件进行射线检测的,但是检测结果与点击事件检测的结果不同,截图如下:

Cocos Creator 版本: 3.8.0