【cocos creator】2.x里,使用3D射线碰撞检测

注意点:

1、开启射线检测

cc.director.getPhysics3DManager().enabled = true;

2: 响应射线的物体要加3D碰撞盒 BoxCollider3D

3:响应射线的物体分组group要设置为3D,分组管理里需要勾选3D和3D的碰撞

demo下载:

https://download.csdn.net/download/K86338236/89527924

typescript 复制代码
const { ccclass, property } = cc._decorator;

@ccclass
export default class CameraTouchCtrl extends cc.Component {

    @property(cc.Camera)
    camera: cc.Camera = null;//3D相机

    @property(cc.Node)
    touchNode: cc.Node = null;//2D触摸节点
    start() {
        //开启射线检测
        cc.director.getPhysics3DManager().enabled = true;
        this.touchNode.on(cc.Node.EventType.TOUCH_START, this.onTouchStart, this);
    }

    /**检测射线 */
    onTouchStart(event) {
        let touchLoc = event.touch.getLocation();
        let ray = this.camera.getComponent(cc.Camera).getRay(touchLoc);
        let maxDistance = 1000;//检测的最远距离,如果物体远,需要改大数值
        let rayColliderGroupName = "3D";//碰撞分组,分组管理里需要勾选3D和3D动碰撞
        //响应射线的物体要加3D碰撞盒 BoxCollider3D,分组group设置为3D
        const result = cc.director.getPhysics3DManager().raycastClosest(ray, rayColliderGroupName, maxDistance, false);
        if (result) {
            let box = result.collider.node
            box.scale += 0.01
        }
    }
}
相关推荐
threelab10 小时前
Three.js 初中数学函数可视化 | 三维可视化 / AI 提示词
开发语言·前端·javascript·人工智能·3d·着色器
轻口味1 天前
HarmonyOS 6.1 全栈实战录 - 10 极光星图:Map Kit 6.1 3D地球、城市灯光与Marker碰撞深度实战
3d·华为·harmonyos
a1117761 天前
边缘设备3DGS-SLAM算法对比实验报告
算法·3d
AI前沿资讯1 天前
2026 AI 3D工具推荐:V2Fun如何重新定义“一站式角色创作”
人工智能·3d
ZPC82102 天前
双目相机 + RGB-D + 点云:生成物体 3D 包围盒 + 计算抓取姿态
数码相机·3d
CG_MAGIC2 天前
3D 建模比例把控技巧
3d·效果图·建模教程·渲云渲染
墨绿色的摆渡人2 天前
论文笔记(一百一十五)Physically Embodied Gaussian Splatting: ... 3D Representation for Robotics (一)
论文阅读·3d
点PY2 天前
Anywhere3D-Bench论文精读
3d
3D小将2 天前
3D格式转换之IFC格式转换为GLTF格式技术文档
3d·solidworks模型·rhino模型·sketchup模型·igs模型
CG_MAGIC2 天前
Enscape 新手从零到出图全流程
3d·贴图·uv·建模教程·渲云渲染