@react three/cannon hooks将matrixAutoUpdate的值更改为false



我很难让相机跟踪一个物体。该对象正在使用来自@react three/cannon的钩子useSphere。我的球体可以很好地移动,但相机不会跟随。我把它缩小到我的球体位置没有更新,因为matrixAutoUpdate被设置为false。我不确定为什么会发生这种情况,因为在我测试的其他Cannon例子中,matrixAutoUpdate保持不变。任何想法都会很棒,我已经被困了一段时间,在互联网上找不到任何东西。下面是一些代码

import React from "react";
import {useFrame, useThree} from "@react-three/fiber";
import {useKeyboardControls} from "../hooks/useKeyboardControls";
import {FPVControls} from "./FPVControls";
import {Vector3} from "three";
import {useSphere} from "@react-three/cannon";
const SPEED = 6
export const Player = (props) => {
const {camera} = useThree()
//sphere movement controls
const {
moveForward,
moveBackward,
moveLeft,
moveRight,
jump
} = useKeyboardControls()

const [ref, api] = useSphere(() => ({
mass: 1,
type: 'Dynamic',
...props
}))

useFrame(() => {
camera.position.copy(ref.current.position)
const direction = new Vector3()
const frontVector = new Vector3(
0,
0,
(moveBackward ? 1 : 0) - (moveForward ? 1 : 0)
)
const sideVector = new Vector3(
(moveLeft ? 1 : 0) - (moveRight ? 1 : 0),
0,
0
)
direction
.subVectors(frontVector, sideVector)
.normalize()
.multiplyScalar(SPEED)
.applyEuler(camera.rotation)
api.velocity.set(direction.x, 0, direction.z)
})
console.log("ref position", ref)
return (
<>
<FPVControls/>
<mesh ref={ref}>
<sphereGeometry  />
<meshStandardMaterial color={"#f30707"} />
</mesh>
</>
)
}

我发现这对类似的问题很有帮助,也许它可以帮助你:

关于类似问题的对话

这个类似的问题是关于一个位置没有通过使用cannon钩子来更新,关键是订阅api位置,并根据我的理解将其存储在ref中。

下面是AI组件中一个更精确的例子(第69行,您可以在第79行找到参考(:

使用示例

相关内容

最新更新