我就是想让场景上的一个cube对象能跟随鼠标运动。
但转换的始终很奇怪,想找大佬帮忙看看,万分感谢啊~~~~ ~。~
代码如下:
systemEvent.on(SystemEventType.TOUCH_START, this.onTouch, this);
onTouch (touch: Touch, event: EventTouch) {
const location = touch.getLocation();
const screenPos = new Vec3(location.x, location.y, 0);
const pos = new Vec3();
// 获取 关联相机的相机数据
const uiCamera= this.node.getComponent(Camera);
// 利用相机数据对象将屏幕点转换成世界坐标下的值
uiCamera.screenToWorld(screenPos,pos);
pos.z = 1;
this.cubeNode.setWorldPosition(pos);
}
当按下时,希望是cube能依附到当前的点击位置。
但是上面的代码导致cube的显示位置和点击的位置差了很多很多。