I am using 3D (Three.js) and physics (Rapier), and I want to control the box movement like a car, with arrow keys, but I have difficulty fixing the movement in the right direction with physics.
Here is a code example https://codesandbox.io/s/q-stackoverflow-9j4sf1
What do I need to insert inside useFrame?
useFrame:
useFrame((state, delta) => {
if (ref.current) {
const box = ref.current;
/// what i should insert in this area
// w/ArrpUP click
if (controllers.forward.isPress) {
// What do I need to insert so that the box moves forward
}
// s/ArrpDown click
if (controllers.backward.isPress) {
// What do I need to insert so that the box moves backward
}
// L/ArrpLeft click
if (controllers.left.isPress) {
// What do I need to insert so that the box rotate left
}
// R/ArrpRight click
if (controllers.right.isPress) {
// What do I need to insert so that the box rotate right
}
}});
The Kinematic Character Controller is a higher-level tool that will emit the proper ray-casts and shape-casts to adjust the user-defined trajectory based on obstacles. Character controller guide
Use the DynamicRayCastVehicleController class for a more realistic vehicle simulation: DynamicRayCastVehicleController
Here is a basic JS example of the Character Controller using your
useFrameloop function. You may want to replace theCuboidwith aCapsulefor smoother results.