Create a burst fire gun or enemy spawner

O

Obj_Neil

Guest
GM Version: Gamemaker 2
Target Platform: ALL
Download: N/A
Links: N/A

Summary:
A way to code a burst fire gun/torrent, which can also be used to spawn enemies. You can control how many bullets to shoot, how fast to shoot them, and how long to wait between shooting.

Tutorial:
If you want detailed step by step instructions, check out the Youtube video here.
I am sure there are many ways to code this, but I couldn't find anything so I came up with this easy way. It gives full control over every aspect of how it shoots.

First create a bullet spawner object and call it what you want. Inside the bullet_spawner's CREATE, write the following code.

Code:
burst_count = 0; // How many bullets to create.
fire_rate = room_speed / 4; // How fast to create bullets.
cool_down = room_speed * 2;  // How long to wait between firing burst of bullets.


// Sets the alarm to run once.
alarm[0] = room_speed / 4;
Inside the bullet_spawner's ALARM[0], write the following code.

Code:
if burst_count < 4 // Creates 4 bullets.
{
// Create the bullet. Be sure to create a layer for the bullet. I chose Fireball_shoot.
// or instead of o_bullet, change this to an enemy, like o_zombie and turn it into an enemy spawner.
instance_create_layer(x, y, "Fireball_shoot", o_bullet);
// Everytime we run through this code, add one to burst_count.
burst_count++;
}

// Reset alarm[0].
alarm[0] = fire_rate;


// A looping alarm to set burst_count back to 0, which we do in alarm[1].
if alarm[1] = -1
{
alarm[1] = cool_down;
}
Inside bullet_spawner's ALARM[1], write the following code.

Code:
// Set burst_count back to 0.
burst_count = 0;
That's it. Be sure to give your bullet a speed and a direction. Here is my bullets code, so that each bullet goes to where my player was. If you want the bullets to home in on your player, i.e. to keep following your player, then place the code in the step event. Or place it inside an alarm, so that the bullet/missle is destroyed after 5 seconds or something.

Inside the bullet's CREATE.

Code:
// set the direction of the bullet to move to where the player was, xx and yy.
direction = point_direction(x, y, o_player.x, o_player.y);
speed = 6;
Be sure to destroy your bullet on collision with player, walls, etc. I do not show how to do that here.

If you have any questions, let me know. If you have a way to improve this code, let me know.
 
Top