SOLVED Top Down Shooter - Strafing Problems

Smidy3

Member
Hey Everyone

New here to the world of gml and gamemaker.

Im making a top down sci fi shooter;

The players ship (Proto1) accelerates forward with "W" and reverses with "S"
"A" & "D" rotate the ship.
and i want "Q" & "E" to strafe left and right, but for the love of me i cannot seem to get it work, i think im just burnt out/overthinking it. And i just need a second pair of eyes.

Values
maxspeed = 5
reversespeed = 3
strafe_speed = 3
minmaxturn = 3

GML:
direction = image_angle;
//forward 
if (keyboard_check(ord("W")))
{
    if (speed < proto1.maxspeed)
    {
        speed += 0.1;
        friction = 0;
    }
}
else {
    if !(keyboard_check(ord("S")))
    if !(keyboard_check(ord("Q")))
    if !(keyboard_check(ord("E")))
    {
        if (speed > 0)
        {
        friction = 0.1;
        }
    }
}
//backward
if (keyboard_check(ord("S")))
{
    if (speed > -proto1.reversespeed)
    {
        speed -= 0.1;
        friction = 0;
     }
}
else {
    if !(keyboard_check(ord("W")))
    if !(keyboard_check(ord("Q")))
    if !(keyboard_check(ord("E")))
    {
        if (speed < 0)
        {
        friction = 0.1;
        }
}
}
//strafe left
if (keyboard_check(ord("Q")))
{
    {
    if (keyboard_check_pressed ("Q"))
        direction -= 90;   
    }       
if (speed < proto1.strafe_speed)
    {
    speed -= 0.1;
    friction = 0;
    }
}
else {
    if !(keyboard_check(ord("S")))
    if !(keyboard_check(ord("W")))
    if !(keyboard_check(ord("E")))
    {
        if (speed > 0)
        {
        friction = 0.1;
        }
}
}
//strafe right
if (keyboard_check(ord("E")))
{
    {
    if (keyboard_check_pressed ("E"))
        direction += 90;   
    }       
if (speed < proto1.strafe_speed)
    {
    speed -= 0.1;
    friction = 0;
    }
}
else {
    if !(keyboard_check(ord("S")))
    if !(keyboard_check(ord("W")))
    if !(keyboard_check(ord("E")))
    {
        if (speed > 0)
        {
        friction = 0.1;
        }
}
}
//rotate left
if (keyboard_check(ord("A")))
    {
    image_angle += proto1.minmaxturn
    if image_angle > 360
        image_angle = 0
}
//roate right
if (keyboard_check(ord("D")))
    {
    image_angle -= proto1.minmaxturn
    if image_angle < 0
        image_angle = 360
}

Please help me :)
 

Nidoking

Member
I think if you want to do this, you'd better get away from using the built-in direction/speed and image_angle variables and handle your own movement. You've tied direction to image_angle explicitly, so trying to make them function independently isn't going to work.
 
P

ParodyKnaveBob

Guest
Partly for your assistance, and partly for my brain's sake, lemme show you some slight code reduction here...

GML:
direction = image_angle;

var _is_W = keyboard_check(ord("W")),
    _is_A = keyboard_check(ord("A")),
    _is_S = keyboard_check(ord("S")),
    _is_D = keyboard_check(ord("D")),
    _is_Q = keyboard_check(ord("Q")),
    _is_E = keyboard_check(ord("E"));

//forward
if (_is_W)
{
    if (speed < proto1.maxspeed)
    {
        speed += 0.1;
        friction = 0;
    }
}
else if (!_is_S && !_is_Q && !_is_E && speed > 0)
{
    friction = 0.1;
}

//backward
if (_is_S)
{
    if (speed > -proto1.reversespeed)
    {
        speed -= 0.1;
        friction = 0;
     }
}
else if (!_is_W && !_is_Q && !_is_E && speed < 0)
{
    friction = 0.1;
}

//strafe left
if (_is_Q)
{
    if (keyboard_check_pressed ("Q"))
    {
        direction -= 90;  
    }
    if (speed < proto1.strafe_speed)
    {
        speed -= 0.1;
        friction = 0;
    }
}
else if (!_is_S && !_is_W && !_is_E && speed > 0)
{
    friction = 0.1;
}

//strafe right
if (_is_E)
{
    if (keyboard_check_pressed ("E"))
    {
        direction += 90;
    }
    if (speed < proto1.strafe_speed)
    {
        speed -= 0.1;
        friction = 0;
    }
}
else if (!_is_S && !_is_W && !_is_E && speed > 0) //! PROBABLE BUG: since you check keyboard_check(ord("E")) twice, I'm guessing that's an accident and that you'd prefer to check keyboard_check(ord("Q")) here
{
    friction = 0.1;
}

//rotate left
if (_is_A)
{
    image_angle += proto1.minmaxturn;
    if (image_angle > 360)
    {
        image_angle = 0; //! POTENTIAL BUG: setting instead of adding/subtracting/modulo'ing
    }
}

//rotate right
if (_is_D)
    {
    image_angle -= proto1.minmaxturn;
    if (image_angle < 0)
    {
        image_angle = 360; //! POTENTIAL BUG: setting instead of adding/subtracting/modulo'ing
    }
}
In cleaning up indentation and braces, I also saw some potential places you might've thought the if..then was attached to one thing when it was attached to another. ~shrug~ Look through, see if my raw rewrite still says what you thought you wanted to say.
 
Top