2D Shooting Mechanic

T

Tortuga

Guest
Hey Everyone, let me first start by saying that I'm fairly new to the whole GameMaker Community, so forgive me if I'm not completely clear when explaining my problem. Anyway I've been watching Shaun Spalding's youtube video tutorials and noticed that there aren't any guides on strictly two dimensional shooting (think Megaman for example). I tried searching all over the Internet but have only turn up mouse-based shooting mechanics. If there is anyone that knows something about 2D Shooting, I'd definitely appreciate the help.
 

samspade

Member
Hey Everyone, let me first start by saying that I'm fairly new to the whole GameMaker Community, so forgive me if I'm not completely clear when explaining my problem. Anyway I've been watching Shaun Spalding's youtube video tutorials and noticed that there aren't any guides on strictly two dimensional shooting (think Megaman for example). I tried searching all over the Internet but have only turn up mouse-based shooting mechanics. If there is anyone that knows something about 2D Shooting, I'd definitely appreciate the help.
I haven't played a lot of the Megaman games. My understanding is that you can only shoot left or right? Possibly up or down. Regardless, if your goal is to create shooting that doesn't use the mouse it is fairly straight forward.

Instance one: Shooting only in the horizontal direction of movement (left or right).

Code:
///event that creates the bullet

with (instance_create(x, y, obj_bullet)) {
    spd = 5;
    facing = other.facing;    //which I believe Shaun's tutorial tracts, but it is basically if (hsp != 0) facing = sign(hsp);
}


///bullet object step event

x += spd * facing;
That is really all you need. Adding in other directions is a little more complicated, but also not too hard. If I remember right, you should just be able to use the bullet code in Shaun Spalding's tutorial but instead of setting the direction to point towards the mouse, simply set it based upon which keys you're pressing. But the basic idea is the same as the only left or right version. Create a bullet object with a few variables that can move it in the direction you want, then when you create the bullet, set those variables.
 
T

Tortuga

Guest
I haven't played a lot of the Megaman games. My understanding is that you can only shoot left or right? Possibly up or down. Regardless, if your goal is to create shooting that doesn't use the mouse it is fairly straight forward.

Instance one: Shooting only in the horizontal direction of movement (left or right).

Code:
///event that creates the bullet

with (instance_create(x, y, obj_bullet)) {
    spd = 5;
    facing = other.facing;    //which I believe Shaun's tutorial tracts, but it is basically if (hsp != 0) facing = sign(hsp);
}


///bullet object step event

x += spd * facing;
That is really all you need. Adding in other directions is a little more complicated, but also not too hard. If I remember right, you should just be able to use the bullet code in Shaun Spalding's tutorial but instead of setting the direction to point towards the mouse, simply set it based upon which keys you're pressing. But the basic idea is the same as the only left or right version. Create a bullet object with a few variables that can move it in the direction you want, then when you create the bullet, set those variables.
Okay I tried the code you showed but I'm only able to shoot to the right, I'll try to mess around with the code and see if I can fix that. If you have anymore suggestions though I'm willing to hear them. Also,i'm pretty sure the instance_create function does not exist in GameMaker Studio 2, all I'm getting is instance_create_layer and instance_create_depth.
 

samspade

Member
Okay I tried the code you showed but I'm only able to shoot to the right, I'll try to mess around with the code and see if I can fix that. If you have anymore suggestions though I'm willing to hear them. Also,i'm pretty sure the instance_create function does not exist in GameMaker Studio 2, all I'm getting is instance_create_layer and instance_create_depth.
If you can only shoot right, it's probably because facing isn't being set correctly. If you're going left, facing (dir, whatever variable you're using) should be -1. I would add the following line show_debug_message(string(spd * facing)); in the bullet step event and see what number it puts out. Should be positive for right and negative for left. And yes, in GMS 2, it is instance_create_layer (or depth).
 
T

Tortuga

Guest
Alright now everything is working properly Thank you so much for the assist. I spent two weeks trying to figure this out.
 
Top