首页 > 解决方案 > 使用 Raycast 方法检测玩家是否使用 ThreeJs 和 CannonJs 接地

问题描述

我目前正在尝试实现 FPS 控件并且需要检测玩家是在地面上还是在飞行(或在跳跃)。所以我了解到可以使用 ThreeJs 的 Raycaster 方法来检测这一点。但是我该如何实施这种方法呢?

我的试用:

let DOWN_DIRECTION = new THREE.Vector3(0, -1, 0);
this._rayCaster = new THREE.Raycaster(this._player._mesh.position, DOWN_DIRECTION, 0, 10);
this._rayCaster.set(this._player._mesh.position, DOWN_DIRECTION);
const intersect = this._rayCaster.intersectObjects(this._player._scene.children, true);

执行上述代码段后,我对 intersect 值进行了控制台操作,它返回了一个空数组。

原始输出:[] 空数组

预期输出:地面(平面网格)

使用的模型:平面为地面,长方体为玩家。

我是否遗漏了代码片段中的某些内容以使其正常工作?

标签: three.jsraycastingcannon.js

解决方案


推荐阅读