Diagonal Movement with WASD

I’m having a bit of a problem coding a simple WASD movement script using setLinearVelocity. I’m trying to keep the player’s speed constant regardless of what direction it’s moving in, which means compensating for diagonal movement. (For a resultant velocity of 4.0, the x and y axis components are set to sine(45) * 4, or 2.83).

The following script works well, but there’s a bit of a bug- when moving right, the player won’t move diagonally. It works normally when going moving diagonally to the left. I’m not sure why that is; perhaps it has to do with the ordering of the if/elif statements?

if wkey.positive:
    tank.setLinearVelocity((-4.0, 0.0, 0.0), True)
if akey.positive:
    tank.setLinearVelocity((0.0, -4.0, 0.0), True)
if skey.positive:
    tank.setLinearVelocity((4.0, 0.0, 0.0), True)
if dkey.positive:
    tank.setLinearVelocity((0.0, 4.0, 0.0), True)
elif wkey.positive and akey.positive:
    tank.setLinearVelocity((-2.83, -2.83, 0.0), True)
elif akey.positive and skey.positive:
    tank.setLinearVelocity((2.83, -2.83, 0.0), True)
elif skey.positive and dkey.positive:
    tank.setLinearVelocity((2.83, 2.83, 0.0), True)
elif dkey.positive and wkey.positive:
    tank.setLinearVelocity((-2.83, 2.83, 0.0), True)

All of the elif statements come after the final if statement - if the D-key isn’t being pressed, then the diagonals are all considered. You need to change the statements to normal if-statements.

Thanks, SolarLune, it works. That definitely makes sense now- I used “elif” on the diagonal directions because I thought they had to have some kind of priority over the single directions. Seems like I was wrong. XD Thanks again!