RigidBody ref prop in latest version (2.1.0) #762
Breyner-QuePar
started this conversation in
General
Replies: 1 comment
-
Hi, this looks like a duplicate of #758 Can you confirm your problem is resolved after actioning the notice in the readme?
|
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Hi guys
I just finished to implement a rapier example like this
import { Canvas } from "@react-three/fiber";
import { Suspense, useEffect, useRef, useState } from "react";
import { Physics, RigidBody, RapierRigidBody } from "@react-three/rapier";
import { OrbitControls, Box } from "@react-three/drei";
import * as THREE from "three";
const Model = () => {
const cubeRef = useRef(null);
const [hover, setHover] = useState(false);
useEffect(() => {
if (cubeRef.current) {
console.log("RigidBody is initialized:", cubeRef.current);
} else {
console.warn("RigidBody is not initialized yet.");
}
}, []);
const jump = () => {
if (cubeRef.current) {
const impulse = new THREE.Vector3(0, 5, 0);
cubeRef.current.applyImpulse(impulse, true);
}
console.log("Jumping!");
};
return (
<>
<RigidBody ref={cubeRef} colliders="cuboid" position={[-2.5, 1, 0]}>
<Box
onClick={jump}
onPointerEnter={() => setHover(true)}
onPointerLeave={() => setHover(false)}
>
<meshStandardMaterial color={hover ? "hotpink" : "royalblue"} />
);
};
export const PhysicsModel = () => {
return (
<Canvas
shadows
camera={{ position: [10, 10, 10], fov: 30 }}
style={{ height: "100vh", width: "100vw" }}
>
);
};
And I found that the ref prop on RigidBody component has a bug even trying everything (useEffect to wait for mounting the component, setTimeout, setInterval, etc.) the ref is always null. This happened in the latest version (2.1.0). I resolved it just to downgrade the version to v.1.5.0. This is my whole package.json
"dependencies": {
"@headlessui/react": "^2.2.0",
"@heroicons/react": "^2.2.0",
"@react-three/drei": "^9.121.4",
"@react-three/fiber": "^8.17.14",
"@react-three/rapier": "^2.1.0" to "1.5.0"
"curvedpath":
"file:",
"gsap": "^3.12.7",
"install": "^0.13.0",
"lamina": "^1.1.23",
"npm": "^11.2.0",
"react": "^18.3.1",
"react-dom": "^18.3.1",
"react-router-dom": "^7.4.0",
"three": "^0.176.0",
"three-stdlib": "^2.36.0"
}
I put this here just if someone is struggling with the same issue
Pdta: Sorry with the post code format I didn't why this is happening
Beta Was this translation helpful? Give feedback.
All reactions