-
Notifications
You must be signed in to change notification settings - Fork 163
Open
Description
👋 Hi there, I was learning react-three-fiber with https://youtu.be/ZnXKmODEFHA?t=961
When I'm try to use camera.position.copy(ref.current.position) to move with the sphere I just created, I found ref.current.position doesn't update.
Here's my code:
export const User = () => {
// user Sphere
const { camera } = useThree()
const [ref, api] = useSphere(() => ({
mass: 1,
type: 'Dynamic',
position: [0, 10, 0],
}))
// get movement
const { moveForward, moveBackward, moveLeft, moveRight } = useKeyboardControls()
// initial velocity
const velocity = useRef([0, 0, 0])
// subscribe to properties to get updates on each frame.
useEffect(() => {
const unsubscribe = api.velocity.subscribe((v) => velocity.current = v)
return unsubscribe
}, [])
useFrame(() => {
camera.position.copy(ref.current.position)
// get direction
const direction = new Vector3()
// horizontal direction, z-direction
const frontVector = new Vector3(0, 0, Number(moveBackward) - Number(moveForward))
// horizontal direction, x-direction
const sideVector = new Vector3(Number(moveLeft) - Number(moveRight), 0, 0)
// vertical, y-direction
const SPEED = speedUp ? 15 : 5
// apply horizontal movement to direction
direction
.subVectors(frontVector, sideVector)
.normalize()
.multiplyScalar(SPEED)
.applyEuler(camera.rotation)
api.velocity.set(direction.x, velocity.current[1], direction.z)
})
return (
<>
<mesh ref={ref}>
<sphereGeometry />
</mesh>
</>
)
}
and my dependency:
"@react-three/cannon": "^6.3.0",
"@react-three/drei": "^9.4.3",
"@react-three/fiber": "^8.0.11",
Glavin001, name1ess0ne and steelx
Metadata
Metadata
Assignees
Labels
No labels

