cocos 触摸2d屏幕拖动3d节点思路

根据屏幕点击的2d坐标,以及3d摄像机,生成一条带方向的3d射线检测,被射线碰撞的物体,就可以获取到射线碰撞到3d物体上的具体坐标点,然后根据这个坐标点设置被拖动3d节点的位置。

核心代码:

javascript 复制代码
//触摸移动
    onTouchMove(event: EventTouch) {

        //射线检测是否碰到其他方块,并修改显示颜色。
        let touchPos = event.getLocation();
        console.log('触摸移动:', touchPos,event.touch);
        if (!this.dragNode) { //有拖拽节点才进行移动。
            return;
        }
        //拖拽
        let point = new Vec3(1, 1, 0);
        // this.dragNode.setPosition(point);

        let ray = this.mainCamera.screenPointToRay(touchPos.x, touchPos.y);
        // console.log('ray',ray,PhysicsSystem.instance.raycastClosest(ray))
        if (PhysicsSystem.instance.raycastClosest(ray)) {
            const res = PhysicsSystem.instance.raycastClosestResult;
            console.log('移动位置:',res.hitPoint)
            const hitNode = res.collider.node;
            // this.dragNode.setPosition(res.hitPoint.x,res.hitPoint.y,res.hitPoint.z);
            this.dragNode.setPosition(res.hitPoint.x,1,res.hitPoint.z);
            if (hitNode.name.startsWith('tiled')) {
                this.cancelTiled();
                hitNode.getComponent(MeshRenderer).material = this.tiledImgActive;
                this.tiledNodeActive = hitNode;

            } else {
                this.cancelTiled();
            }
        } else {
            this.cancelTiled();
        }
    }

核心属性:res.hitPoint

相关推荐
碧口科技7 小时前
AI3D模型生成会不会结构不完整?判断标准与修复流程
人工智能·3d
城中南小15 小时前
3D-VLA 方法原理详解
人工智能·计算机视觉·3d
劉宇雁18 小时前
C++ ASCII 3D无尽跑酷游戏
c++·游戏·3d
资源分享交流19 小时前
TripoSplat 本地整合包:一张图片生成 3D Gaussian 模型
3d·ai
DolitD1 天前
无人机×云渲染:点量云流实时交互云推流方案
3d·云计算·数据可视化·虚拟现实
云飞云共享云桌面2 天前
单机建模卡顿运维繁琐!中小型机械制造厂云飞云 3D 云桌面落地
运维·服务器·网络·3d·自动化·电脑·负载均衡
2601_956414142 天前
AI3D模型生成工具怎么比较?看生成质量、后续流程和导出能力
人工智能·3d
私人珍藏库2 天前
[Android] VOKA -人体解剖3D图谱+医学人体解剖
3d·智能手机·app·工具·软件·多功能
2601_956414143 天前
AI3D模型生成工具怎么比较?2026主流平台对比与实测维度解析
人工智能·3d
蓝速科技3 天前
蓝速科技视觉 3D 全息仓 AI 数字人一体机深度评测
人工智能·科技·3d