S
Spencer S
Guest
So I have a script that the player executes that controls movement.
playerRunSpeed is set to 90.
playerDiagonalRunSpeed is set to 63.
xAcceleration is set to 6.
yAcceleration is set to 6.
yCurrentSpeed is updated every step of the game based on what has happened to it before.
xCurrentSpeed is the same way, but for the x axis.
The bugs are this: when I hold down up AND right, or down AND right, the y axis never stops accelerating as long as a button is held down, and the x axis stops accelerating completely until all buttons are released. And the player will skid to a stop if they are moving right or down, but not left or up. If they are moving left, or up, they stop immediately. AND, if I press up AND right, or down AND right, and then release right, the player still moves at the same speed before releasing the button until I release all keys.
Tl;dr there's a lot of weird interactions when holding down multiple buttons, and the player won't skid to a stop when moving left or up and all buttons are released. What are my flaws?
playerRunSpeed is set to 90.
playerDiagonalRunSpeed is set to 63.
xAcceleration is set to 6.
yAcceleration is set to 6.
yCurrentSpeed is updated every step of the game based on what has happened to it before.
xCurrentSpeed is the same way, but for the x axis.
The bugs are this: when I hold down up AND right, or down AND right, the y axis never stops accelerating as long as a button is held down, and the x axis stops accelerating completely until all buttons are released. And the player will skid to a stop if they are moving right or down, but not left or up. If they are moving left, or up, they stop immediately. AND, if I press up AND right, or down AND right, and then release right, the player still moves at the same speed before releasing the button until I release all keys.
Tl;dr there's a lot of weird interactions when holding down multiple buttons, and the player won't skid to a stop when moving left or up and all buttons are released. What are my flaws?
Code:
if key_up {
if (yCurrentSpeed < playerRunSpeed) {
yCurrentSpeed -= yAcceleration;
}
else {
yCurrentSpeed = playerRunSpeed;
}
}
else if key_left {
if (xCurrentSpeed < playerRunSpeed) {
xCurrentSpeed -= xAcceleration;
}
else {
xCurrentSpeed = playerRunSpeed;
}
}
else if key_down {
if (yCurrentSpeed < playerRunSpeed) {
yCurrentSpeed += yAcceleration;
}
else {
yCurrentSpeed = playerRunSpeed;
}
}
else if key_right {
if (xCurrentSpeed < playerRunSpeed) {
xCurrentSpeed += xAcceleration;
}
else {
xCurrentSpeed = playerRunSpeed;
}
}
else if key_up && key_right {
if (xCurrentSpeed < playerDiagonalRunSpeed) {
xCurrentSpeed += xAcceleration;
}
else if (yCurrentSpeed < playerDiagonalRunSpeed) {
yCurrentSpeed -= yAcceleration;
}
else {
xCurrentSpeed = playerDiagonalRunSpeed;
yCurrentSpeed = playerDiagonalRunSpeed;
}
}
else if key_up && key_left {
if (xCurrentSpeed < playerDiagonalRunSpeed) {
xCurrentSpeed -= xAcceleration;
}
else if (yCurrentSpeed < playerDiagonalRunSpeed) {
yCurrentSpeed -= yAcceleration;
}
else {
xCurrentSpeed = playerDiagonalRunSpeed;
yCurrentSpeed = playerDiagonalRunSpeed;
}
}
else if key_down && key_left {
if (xCurrentSpeed < playerDiagonalRunSpeed) {
xCurrentSpeed -= xAcceleration;
}
else if (yCurrentSpeed < playerDiagonalRunSpeed) {
yCurrentSpeed += yAcceleration;
}
else {
xCurrentSpeed = playerDiagonalRunSpeed;
yCurrentSpeed = playerDiagonalRunSpeed;
}
}
else if key_down && key_right {
if (xCurrentSpeed < playerDiagonalRunSpeed) {
xCurrentSpeed += xAcceleration;
}
else if (yCurrentSpeed < playerDiagonalRunSpeed) {
yCurrentSpeed += yAcceleration;
}
else {
xCurrentSpeed = playerDiagonalRunSpeed;
yCurrentSpeed = playerDiagonalRunSpeed;
}
}
else if (!key_up) && (!key_left) && (!key_down) && (!key_right) {
if (xCurrentSpeed > 0) {
xCurrentSpeed -= xAcceleration;
}
else if (yCurrentSpeed > 0) {
yCurrentSpeed -= yAcceleration;
}
else {
yCurrentSpeed = 0;
xCurrentSpeed = 0;
}
}
// Move the player
x += xCurrentSpeed;
y += yCurrentSpeed;