game-makergmlgame-maker-studio-2game-maker-language

Game Maker WASD and arrow keys smooth rotation?


How can I make smooth rotation using WASD or arrow keys? I saw some exaples, but they all use mouse. I surely want to use WASD or arrow keys. I really hope someone can help me with this.

This is my code so far.

// You can write your code in this editor
if(keyboard_check_pressed(ord("W"))) or (keyboard_check_pressed(vk_up)){
    speed = sailSpeed;
    image_speed = 1;
}
if(keyboard_check_released(ord("W"))) or (keyboard_check_released(vk_up)){
    speed = 0;
    image_speed = 0;
    image_index = 0;
}
if(keyboard_check_pressed(ord("D"))) or keyboard_check_pressed(vk_right){
    direction -= 45;
}
if(keyboard_check_pressed(ord("A"))) or keyboard_check_pressed(vk_left){
    direction += 45;
}

if(direction == 0) or (direction == 360) or (direction == -360){
    sprite_index = sprPlayerShipRight;
}
if(direction == 45) or (direction == -315){
    sprite_index = sprPlayerShipUpRight;
}
if(direction == 90) or (direction == -270){
    sprite_index = sprPlayerShipUp;
}
if(direction == 135) or (direction == -225){
    sprite_index = sprPlayerShipUpLeft;
}
if(direction == 180) or (direction == -180){
    sprite_index = sprPlayerShipLeft;
}
if(direction == 225) or (direction == -135){
    sprite_index = sprPlayerShipDownLeft;
}
if(direction == 270) or (direction == -90){
    sprite_index = sprPlayerShipDown;
}
if(direction == 315) or (direction == -45){
    sprite_index = sprPlayerShipDownRight;
}

Solution

  • If you want to turn by 45 degrees your sprite in a "smoothly" way, the only thing that you can do is to draw, let's say, another 3 mid-frames to the turning. Anyway, the more sprites the more smooth the animation will be. You can't make a smooth turning with just two sprites.

    Don't rely on the turning function of GM, better to draw the frames by yourself, trust me.