• 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!

GML How to get bullets to shoot in direction character is facing? [SOLVED]

S

Scytaic

Guest
Hi, I'm completely new to GMS2 and GML/coding. I've been following Shaun's platform tutorial and managed to change the code a bit so the bullets only shoot in the direction the character is facing (because I don't want my game to use the mouse). I found that specific part of the code online and don't understand it that well to be honest. But I managed to make it so the bullets shoot to the right but they still continue to shoot to the right even when the character is facing left. This is the script I'm using for the gun:

Code:
x = object2.x;
y = object2.y;
if object2.x > x
    {
    if direction = !90
        {                             
        image_angle = direction
        }
    } else
    if object2.x < x
    {
    if direction = !90
        {                             
        image_angle = direction+180
        }
    }
 
firingdelay = firingdelay - 1;
if (keyboard_check_pressed(ord("Z"))) && (firingdelay < 0)
{
    firingdelay = 5;
    with (instance_create_layer(x,y,"bullets",obullet))
    {
        speed = 10;
        direction = other.image_angle + random_range(-3,3);
        image_angle = direction;
    }
}
If I change any of the if object2.x > x (swap the <'s over etc) it'll then only shoot to the left. How do I make it so it only shoots right if facing right and only shoots left if facing left? Sorry, I'm a complete n00b haha. Thanks in advance!
 
Last edited by a moderator:
T

Taddio

Guest
object2.x and x will allways be the same (never < or >) because of your first 2 lines, so there's no way it will work like that :) so your if statement will never run.

There's a lot of ways to do this, but not this one.
direction=image_angle is a good start, tho ;)
 
S

Scytaic

Guest
object2.x and x will allways be the same (never < or >) because of your first 2 lines, so there's no way it will work like that :) so your if statement will never run.

There's a lot of ways to do this, but not this one.
direction=image_angle is a good start, tho ;)
Oh thank you so much I'll do some more testing and editing around! :)
 
T

Taddio

Guest
No problem! Tell me if you can't get it to work, we'll find a work around :)
 
S

Scytaic

Guest
No problem! Tell me if you can't get it to work, we'll find a work around :)
Thanks for helping me! :) I removed the first 2 lines and it works! But... now it doesn't follow the player around :eek: lol
 
T

Taddio

Guest
Do you have different sprites with each facing direction? You could do

if (sprite_index = spr_player_left){
//Shoot left
}
Is it grid-based? Platformer? Can you shoot up/down or just left and right?
 
S

Scytaic

Guest
Do you have different sprites with each facing direction? You could do

if (sprite_index = spr_player_left){
//Shoot left
}
Is it grid-based? Platformer? Can you shoot up/down or just left and right?
Nah I think the characters just flips when the left button is pressed (pretty sure that's what Shaun's tutorial said if I remember right) I think it would make it a lot easier if I did have a different sprite though if I'm able to code that lol. :) it's platformer and can only shoot left and right. :)
 
T

Taddio

Guest
If you flip your character with image_xscale =-1
you could probably do if(obj_player.image_xscale <= -1){
obj_bullet.direction = 180;
} else obj_bullet.direction = 0;

I'm on the phone and at work atm, I'll try to take a look at your code tonight if it still don't work.
Keep it up, bro! Best way to learn is try-and-fail-and-analyse-why-it-doesn't-work!
I'm sure you'll get around this in no time :)

And yeah, having different sprites is cool, and if you just flip your sprite like that, it MAY look weird to some if, for example, your char has a shield in one hand, and a sword in the other. Just flipping it will look as if the sword switches hand every time you turn around.
 
S

Scytaic

Guest
Thank you again, sorry I seriously have 0 knowledge on scripting haha, but the gun still doesn't follow the character around, it just stays in the area it's placed in the beginning. The last script worked well besides that (turning and shooting left and right depending on which way the character faced, yay!) I just put that code in and it still doesn't follow the character and it doesn't move left or right anymore so I guess the last code was on the right path? :)
 
T

Taddio

Guest
Oh! Simply put back the first lines you deleted before your if statement, or in the bullet create event.
x = obj_player.x;
y = obj_player.y;
As your if statement doesnt reference to x>obj_player.x;
it should work and the bullet will spawn where your player is! Just get back to me if it doesn't :)
 
S

Scytaic

Guest
Thank you both! I feel so dumb but no matter what I do (and I've tried a lot lol!) The bullets just won't shoot another direction. :( But thankfully I have a friend who also uses gms2 and I'm going to just show him it and see what he can do haha. Thank you again though for all your help! :D
 

TheouAegis

Member
Thank you both! I feel so dumb but no matter what I do (and I've tried a lot lol!) The bullets just won't shoot another direction. :( But thankfully I have a friend who also uses gms2 and I'm going to just show him it and see what he can do haha. Thank you again though for all your help! :D
Seeing as how you mentioned Shaun's tutorials, things make sense, because there are other people posting on the forums (probably just means I've forgotten about all the other people in the past) who are using the same code and can't get it to work. BECAUSE SHAUN'S TUTORIALS ARE FLAWED. (Some of them are good, to be fair.)

I am assuming this code goes inside a gun object, hence the x=object2.x;y=object2.y lines. Shaun's little "the gun feels like it has some weight to it" comment is really just excusing bad code, although his little sprite is making it work. The faster you move, the farther behind the gun would lag. Trust me, Shaun's "weight" excuse is just that and you should not go along with it. So make sure your code is in the End Step event so that the gun stays attached to object2 like it's supposed to. If you want "weight lag", then do it correctly by setting limits in the End Step event.

So I'm assuming since you have the direction!=90 check, that you are using a normalized gun aiming like direction is 0 or 180 when not aiming, or 90 when aiming up. Am I right?
Code:
image_xscale = object2.image_xscale;
if keyboard_check(vk_right)
    image_xscale = 1;
else
if keyboard_check(vk_left)
    image_xscale = -1;
if keyboard_check(vk_up) {
    direction = 90;
    image_angle = direction;
    //If we were facing left, the gun would be upside down, so flip it around
    if image_xscale < 0 image_angle += 180;
}
else {
    direction = 90-image_xscale*90;
    //We don't want to rotate the gun if we're not aiming
    image_angle = 0;
}
Shooting the bullet then is just
Code:
firing_delay--;
if !firing_delay {
    firing_delay = 5;
    with instance_create_layer(x,y,"bullets",obullet) {
        speed = 10;
        direction = other.direction;
        if direction == 90 
            image_angle = 90;
    }
}
 
S

Scytaic

Guest
It works! Thank you SO much! :D And oh I see, I'm glad I'm not the only one to be honest haha, I was feeling pretty stupid and was about to give up lol. Now I can finally continue making the game! :) Thank you both for your patience and help, I think I'll probably stay away from his tutorials from now on lol. :)
 
Top