im having issues with controlling a character in the local space instead of the world space, is there anyone who can take a look or advice? its based in R3F but would work in standard three.js: useFrame((state, delta) => {
if (rigidbody.current && gameStarted) {
// Update time since last ground contact
const impulse = { x: 0, y: 0, z: 0 };
if (jumpPressed && isOnFloor.current) {
impulse.y += JUMP_FORCE;
isOnFloor.current = false;
}
const linvel = rigidbody.current.linvel();
let changeRotation = false;
if (rightPressed && linvel.x < MAX_VEL) {
impulse.x += MOVEMENT_SPEED;
changeRotation = true;
}
if (leftPressed && linvel.x > -MAX_VEL) {
impulse.x -= MOVEMENT_SPEED;
changeRotation = true;
}
if (backPressed && linvel.z < MAX_VEL) {
impulse.z += MOVEMENT_SPEED;
changeRotation = true;
}
if (forwardPressed && linvel.z > -MAX_VEL) {
impulse.z -= MOVEMENT_SPEED;
changeRotation = true;
}
rigidbody.current.applyImpulse(impulse, true);
if (Math.abs(linvel.x) > RUN_VEL || Math.abs(linvel.z) > RUN_VEL) {
if (characterState !== "Run") {
// dispatch(setIdle());
dispatch(setRun());
}
} else {
if (characterState !== "Idle") {
dispatch(setIdle());
}
}
if (changeRotation) {
const angle = Math.atan2(linvel.x, linvel.z);
character.current.rotation.y = angle;
}