• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

Top Down Character Movement

I

IndieCrypt

Guest
Hello,
I need help with some top down movement... I just cannot get it to work!

I need a character to point in the direction the players has pressed, for example 'A' makes the player turn left and 'D' makes the player turn right. Along with this I want it to, when I press 'W', the character moves towards the players direction and when I press 'S' it moves backwards...
 
S

Shariku Onikage

Guest
Do you mean rotate left or face left?

If face left/face right:

Code:
if keyboard_check(ord('A')){
     speed=0;
     direction = 180;
}

if keyboard_check(ord('D')){
     speed=0;
     direction = 0;
}
If rotate left/rotate right (tank controls)
Code:
if keyboard_check(ord('A')){
     image_angle--;
     speed=0;
}

if keyboard_check(ord('D')){
     image_angle ++;
     speed=0;
If both cases move forwards/move backwards

Code:
if keyboard_check(ord('W')){
     direction= image_angle;
     speed=2;
}

if keyboard_check(ord('S')){
     direction = image_angle-180;
     speed=2;
}
 
I

IndieCrypt

Guest
Do you mean rotate left or face left?

If face left/face right:

Code:
if keyboard_check(ord('A')){
     speed=0;
     direction = 180;
}

if keyboard_check(ord('D')){
     speed=0;
     direction = 0;
}
If rotate left/rotate right (tank controls)
Code:
if keyboard_check(ord('A')){
     image_angle--;
     speed=0;
}

if keyboard_check(ord('D')){
     image_angle ++;
     speed=0;
If both cases move forwards/move backwards

Code:
if keyboard_check(ord('W')){
     direction= image_angle;
     speed=2;
}

if keyboard_check(ord('S')){
     direction = image_angle-180;
     speed=2;
}
Ah I now see what I have been doing wrong... I have been adding to the 'x' axis instead of adding to the speed therefore no matter which way the character would rotate he would always go in the same direction... Thank you, haha!
 
S

Shariku Onikage

Guest
Ah I now see what I have been doing wrong... I have been adding to the 'x' axis instead of adding to the speed therefore no matter which way the character would rotate he would always go in the same direction... Thank you, haha!
Yeah, that would work in a Zelda style game but not for what you wanted here. You may also want to look into hspeed and vspeed (it's not so applicable to what you're currently doing, but it's better for certain types of player movement than using the x and y axis.
 
Top