GameMaker Shooting Left and Right with Spacebar

I'm using GameMaker Studio 2 and I'm having an issue getting a bullet to shoot left and right with a character. I haven't used GM for over a year, but the instance_create used to be pretty easy to use. As well as some of the DnD functions that they have now taken out which used to be super easy and quick to use - especially when teaching the software to students.

But now, I just can't seem to get this function to work the way I need it to.
I managed to find online and get the below code to work - however when I press the space bar to shoot a bullet, it also creates a second instance of the bullet on the screen which stays stationary on the screen. I cannot figure out why.

I've tried doing this multiple ways with the result of the bullet either shooing in only one direction, or it just stays in the one place on the screen, then whenever I press the spacebar again, it puts another bullet, then another, and so on each time I press the spacebar to shoot.

In my obj_bullet - I have the instance of the bullet being destroyed when it leaves the room or when it collides with the wall or enemy. This does not remove the stationary bullets from the screen during game play.

Any suggestions would be greatly appreciated.!!!

The following is located in my obj_player

EVENT = Key Press - Space

//Shooting Bullet
if keyboard_check(vk_left)
{
newBullet=instance_create_layer(x,y,"Bullet",obj_bullet);
newBullet.direction=obj_player.direction;
newBullet.speed=15;
x -=8;
direction=180;

}

if keyboard_check(vk_right)
{
newBullet=instance_create_layer(x,y,"Bullet",obj_bullet);
newBullet.direction=obj_player.direction;
newBullet.speed=15;
x +=8;
direction=0;
}
 

jo-thijs

Member
I'm using GameMaker Studio 2 and I'm having an issue getting a bullet to shoot left and right with a character. I haven't used GM for over a year, but the instance_create used to be pretty easy to use. As well as some of the DnD functions that they have now taken out which used to be super easy and quick to use - especially when teaching the software to students.

But now, I just can't seem to get this function to work the way I need it to.
I managed to find online and get the below code to work - however when I press the space bar to shoot a bullet, it also creates a second instance of the bullet on the screen which stays stationary on the screen. I cannot figure out why.

I've tried doing this multiple ways with the result of the bullet either shooing in only one direction, or it just stays in the one place on the screen, then whenever I press the spacebar again, it puts another bullet, then another, and so on each time I press the spacebar to shoot.

In my obj_bullet - I have the instance of the bullet being destroyed when it leaves the room or when it collides with the wall or enemy. This does not remove the stationary bullets from the screen during game play.

Any suggestions would be greatly appreciated.!!!

The following is located in my obj_player

EVENT = Key Press - Space

//Shooting Bullet
if keyboard_check(vk_left)
{
newBullet=instance_create_layer(x,y,"Bullet",obj_bullet);
newBullet.direction=obj_player.direction;
newBullet.speed=15;
x -=8;
direction=180;

}

if keyboard_check(vk_right)
{
newBullet=instance_create_layer(x,y,"Bullet",obj_bullet);
newBullet.direction=obj_player.direction;
newBullet.speed=15;
x +=8;
direction=0;
}
Hi and welcome to the GMC!

The snippet of code you've posted works perfectly fine for me.

Check if you've "enabled physics" in the properties of your room.
If so, that's your problem, as enabling physics overrides the default speed system.

Otherwise, check if you've got any other code that might be interfering with this.
 

Slyddar

Member
if keyboard_check(vk_left)
{
newBullet=instance_create_layer(x,y,"Bullet",obj_bullet);
newBullet.direction=obj_player.direction;
newBullet.speed=15;
x -=8;
direction=180;

}
I don't see another bullet being generated, so it's most likely happening because of other code.

As a side, this code requires the player to be moving to shoot, i.e. if left or right is not being pressed no bullets are generated. Not sure if that's what you needed.
Also when shooting to the left, why are you moving the player 8 pixels to the left with every shot?

Lastly you might not be aware, but it's more efficient to use a local var when creating an instance, so
Code:
var newBullet=instance_create_layer(x,y,"Bullet",obj_bullet);
 
Thanks for checking the code for me and the updated variable suggestion.
There was some other code that had it generating another bullet in the step event.
 
Top