Skip to content

Commit

Permalink
fix reverse on gamepad and mouse controller
Browse files Browse the repository at this point in the history
  • Loading branch information
ctrlVnt committed Feb 16, 2024
1 parent f19e940 commit e4a4d22
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 4 deletions.
21 changes: 19 additions & 2 deletions src/components/PlayerController.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)
Expand Down
15 changes: 13 additions & 2 deletions src/components/PlayerControllerGamepad.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit e4a4d22

Please sign in to comment.