why does my player move faster when going diagonal than horizontal/vertical?
if you were following my top-down tutorial, you may have noticed that your player moves faster when going diagonal.
the reason for this is simple, take a look at our current code:
move_and_collide(horizontal_input * movement_speed, vertical_input * movement_speed, oSolid)
if the player is moving both vertically and horizontally, we are adding to both the x and y position, and if the player only moves in one direction, it will only add one of those variables.
time for some math:
if the player is moving in two directions at once, but you want the overall speed to be one, by the pythagorean theorem the speed in each directions needs to be around .707
now update your code:
if(x_speed != 0 and y_speed != 0 ) { // if the movement in both directions are not 0
// multiply movement by .707 in both directions
move_and_collide(horizontal_input * movement_speed * .707, vertical_input * movement_speed * .707, oSolid)
} else { // if the movement is only in one direction
// move normally
move_and_collide(horizontal_input * movement_speed, vertical_input * movement_speed, oSolid)
}
now your movement should feel better! this really shows how math can be used in the real world