Help with my code to reduce diagonal speed
GameMaker Version: v1.4.1763
So I make my player move by using the WASD keys according to a global variable that I've set. To simulate coming towards the screen and further away (in an isometric game) I want the vertical speeds to be reduced by a certain percentage, and I multiple the vertical movements' speeds by a certain value (0.75).
However, I don't want the player's diagonal movement to be the the sum of the squares of the x and y variable. To avoid this issue, I determine if my player's speed is greater than his maximum set speed, and if so I divide that axis variable by sqrt(2). This is how I approached the issue:
///Player Controls
// Move, but do not speed up when moving diagonally
if keyboard_check(ord("W")) and MaxPlayerSpeed >= speed { y = y - MaxPlayerSpeed * VerticalSpeedReduction } else if keyboard_check(ord("W")) and MaxPlayerSpeed < speed { y = y - ( MaxPlayerSpeed * VerticalSpeedReduction / sqrt(2)) }
if keyboard_check(ord("S")) and MaxPlayerSpeed >= speed { y = y + MaxPlayerSpeed * VerticalSpeedReduction } else if keyboard_check(ord("S")) and MaxPlayerSpeed < speed { y = y + ( MaxPlayerSpeed * VerticalSpeedReduction / sqrt(2)) }
if keyboard_check(ord("A")) and MaxPlayerSpeed >= speed { x = x - MaxPlayerSpeed } else if keyboard_check(ord("A")) and MaxPlayerSpeed < speed { x = x - ( MaxPlayerSpeed / sqrt(2)) }
if keyboard_check(ord("D")) and MaxPlayerSpeed >= speed { x = x + MaxPlayerSpeed } else if keyboard_check(ord("D")) and MaxPlayerSpeed < speed { x = x + ( MaxPlayerSpeed / sqrt(2)) }
I'm certain that all of my carrots are facing the correct directions. Whenever my player moves diagonally, I notice that the horizontal and vertical speeds don't change to compensate for the diagonal speed despite my code. What am I doing wrong?
Posted Jul 21, 2017, 10:22 PM UTC