【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
        }
    }
}
相关推荐
多恩Stone1 天前
【3D-AICG 系列-14】Trellis 2 的 Texturing Pipeline 保留单层薄壳,而 Textured GLB 会变成双层
人工智能·python·算法·3d·aigc
EQ-雪梨蛋花汤1 天前
【3D可视化】基于 Unity 的智慧体育馆三维信息可视化大屏实践
3d·unity·信息可视化
多恩Stone2 天前
【3D-AICG 系列-11】Trellis 2 的 Shape VAE 训练流程梳理
人工智能·pytorch·算法·3d·aigc
CG_MAGIC2 天前
Blender幕后花絮:王国受袭——叛乱
3d·blender·贴图·建模教程·渲云渲染
多恩Stone2 天前
【3D-AICG 系列-12】Trellis 2 的 Shape VAE 的设计细节 Sparse Residual Autoencoding Layer
人工智能·python·算法·3d·aigc
多恩Stone2 天前
【3D-AICG 系列-13】Trellis 2 的 SC-VAE 的 Training Loss 细节
人工智能·算法·3d·aigc
你好!蒋韦杰-(烟雨平生)3 天前
Opengl模拟水面
c++·游戏·3d
a1117763 天前
3D赛车躲避游戏(html threeJS开源)
前端·游戏·3d·开源·html·threejs
该怎么办呢3 天前
基于 Cesium 3D Tiles 的局部压平方案解析
3d
视觉人机器视觉4 天前
海康机器人3D 机器人引导 —— 空间基础篇一
3d·机器人