onCollisionEnter is not working in r3f how can i fix it?

23 Views Asked by At

I'm creating a map that moves the character with @react-three/fiber and @react-three/rapier. I tried to create a collider on the door and when the player collides there, change isInside to true to make it go inside the building, but oncCollisionEnter() doesn't work.

return (
        <>
        {!isInside&&(
            <RigidBody 
                type="fixed" 
                name="GoInDoor"
                onCollisionEnter={({other}) => {
                    console.log("collision detected")
                    if(other.rigidBodyObject.name==="Player"){
                        console.log("GoInDoor collision with",other.rigidBodyObject.name);
                        setIsinside(true);
                    }}}
            >
                <mesh position={[-15, 60, 500]}>
                    <boxGeometry args={[90, 100, 20]} />
                    <meshStandardMaterial color="red"/>
                </mesh>
            </RigidBody>
            )}
            {isInside&&(
            <RigidBody type="fixed" name="GoOutDoor"
                onCollisionEnter={({other}) => {
                    console.log("collision detected")
                    if(other.rigidBodyObject.name==="Player"){
                        console.log("GoOutDoor collision with",other.rigidBodyObject.name);
                        setIsinside(false);
                    }}}
            >
                <mesh>
                    <boxGeometry
                        args={[100, 100, 10]}
                        position={[-15, 72, 530]}
                    />
                    <meshStandardMaterial color="blue"/>
                </mesh>
            </RigidBody>
            )}
            <RigidBody 
                name="Player"
                ref={characterRigidBody}
                type="kinematicPosition"
                colliders={false}
                enabledRotations={[false,false,false]}
                >
                <CapsuleCollider 
                    args={[13,20]} 
                    ref={characterCollider}
                    position={[0,60,600]}
                />
                <primitive 
                    object={scene}
                    ref={characterRef}
                    scale={28}
                    position={[0,28,600]}
                />
            </RigidBody>
        </>
    );

Even if it colliders, nothing happens on the console. What's wrong with my code?

Changing the type of rigidBody didn't help.

0

There are 0 best solutions below