From e4a4d22374627549ee2a56586953680f674de3f2 Mon Sep 17 00:00:00 2001 From: Riccardo Venturini <80196285+ctrlVnt@users.noreply.github.com> Date: Fri, 16 Feb 2024 14:11:21 +0000 Subject: [PATCH] fix reverse on gamepad and mouse controller --- src/components/PlayerController.jsx | 21 +++++++++++++++++++-- src/components/PlayerControllerGamepad.jsx | 15 +++++++++++++-- 2 files changed, 32 insertions(+), 4 deletions(-) diff --git a/src/components/PlayerController.jsx b/src/components/PlayerController.jsx index 7dffc8f..22c71e2 100644 --- a/src/components/PlayerController.jsx +++ b/src/components/PlayerController.jsx @@ -121,6 +121,12 @@ export const PlayerController = ({ } else if (rightPressed && currentSpeed > 0) { steeringAngle = -currentSteeringSpeed; targetXPosition = camMaxOffset; + } else if (rightPressed && currentSpeed < 0) { + steeringAngle = currentSteeringSpeed; + targetXPosition = -camMaxOffset; + } else if (leftPressed && currentSpeed < 0) { + steeringAngle = -currentSteeringSpeed; + targetXPosition = camMaxOffset; } else { steeringAngle = 0; targetXPosition = 0; @@ -182,13 +188,24 @@ export const PlayerController = ({ } // REVERSING - if (downPressed && currentSpeed < -maxSpeed) { + if (downPressed) { + if (currentSteeringSpeed < MaxSteeringSpeed) { + setCurrentSteeringSpeed( + Math.min( + currentSteeringSpeed + 0.0001 * delta * 144, + MaxSteeringSpeed + ) + ); + } + } + + if (downPressed && currentSpeed <= 0) { setCurrentSpeed( Math.max(currentSpeed - acceleration * delta * 144, -maxSpeed) ); } // DECELERATING - else if (!upPressed && !downPressed) { + else if (!upPressed) { if (currentSteeringSpeed > 0) { setCurrentSteeringSpeed( Math.max(currentSteeringSpeed - 0.00005 * delta * 144, 0) diff --git a/src/components/PlayerControllerGamepad.jsx b/src/components/PlayerControllerGamepad.jsx index 854719a..a37cc15 100644 --- a/src/components/PlayerControllerGamepad.jsx +++ b/src/components/PlayerControllerGamepad.jsx @@ -165,13 +165,24 @@ export const PlayerControllerGamepad = ({ } // REVERSING - if (buttonB && currentSpeed < -maxSpeed) { + if (buttonB) { + if (currentSteeringSpeed < MaxSteeringSpeed) { + setCurrentSteeringSpeed( + Math.min( + currentSteeringSpeed + 0.0001 * delta * 144, + MaxSteeringSpeed + ) + ); + } + } + + if (buttonB && currentSpeed <= 0) { setCurrentSpeed( Math.max(currentSpeed - acceleration * delta * 144, -maxSpeed) ); } // DECELERATING - else if (!buttonA && !buttonB) { + else if (!buttonA) { if (currentSteeringSpeed > 0) { setCurrentSteeringSpeed( Math.max(currentSteeringSpeed - 0.00005 * delta * 144, 0)