SOLVED Hi, beginner here. I have not been able to successfully code my bullet laser i have made to shoot from my ship. Please help?

S

SlFinn

Guest
I have now been using Game Maker and learning to code in GML for 3 days. Someone else on here helped me with something before so I am hoping someone can help with this.
I have tried to code a laser bullet to shoot from my ship, but when I press the space bar that I have assigned to shoot the bullet from the ship, all it does is appear on screen. The bullet does not move at all. I will post below the code I have tried to use.
Please could you tell me where I have went wrong and what I need to do to fix it? I would very much appreciate it!

Within the oPlayer step event script:

GML:
if keyboard_check_pressed(vk_space) {
bullet = instance_create_layer(x + 10, y - 5, "Instances",obj_bullet);
 }
Within my obj_bullet create script:

Code:
speed = 5;
 

Shockman

Member
Did you set a direction for the bullet equal to the direction of the ship, or towards the target if shooting from side, back, or turret?
 

Amon

Member
You need to assign the speed value to either x or y in the step event.

So, in the bullets step event, if you want the bullet to travel up the screen just assign speed to the y axis:

y -= speed;
 

chamaeleon

Member
You need to assign the speed value to either x or y in the step event.

So, in the bullets step event, if you want the bullet to travel up the screen just assign speed to the y axis:

y -= speed;
That is not how the built-in speed variable works. The point of using it is to not update x and y manually, but use speed and direction instead.
 

Nocturne

Friendly Tyrant
Forum Staff
Admin
What the OP has currently should actually be enough to set the bullet in motion, but only to the right of the screen. Setting the speed built-in variable when no direction is set will use the default direction of 0º which is to the right. Therefor it's odd that the bullet itself isn't moving when it is first created, as it should if the speed is being set in the create event. That said...

The code you have currently for creating the bullet is fine, all you need to do is define the direction and speed at the same time, for example:

GML:
if keyboard_check_pressed(vk_space)
{
bullet = instance_create_layer(x + 10, y - 5, "Instances",obj_bullet);
bullet.speed = 5;
bullet.direction = 0; // change this to whatever is required...
}
If, after using this code, the bullet is still not moving then something else is going on...
 
S

SlFinn

Guest
Thanks everyone! I got it to work thanks to you :)
I had to mess around with the speed a bit but I found the right speed eventually :)

What the OP has currently should actually be enough to set the bullet in motion, but only to the right of the screen. Setting the speed built-in variable when no direction is set will use the default direction of 0º which is to the right. Therefor it's odd that the bullet itself isn't moving when it is first created, as it should if the speed is being set in the create event. That said...

The code you have currently for creating the bullet is fine, all you need to do is define the direction and speed at the same time, for example:

GML:
if keyboard_check_pressed(vk_space)
{
bullet = instance_create_layer(x + 10, y - 5, "Instances",obj_bullet);
bullet.speed = 5;
bullet.direction = 0; // change this to whatever is required...
}
If, after using this code, the bullet is still not moving then something else is going on...
Thanks so much. You really helped!

Just rename speed to whatever, spd for instance. Then

y -= spd;
Thanks again! You're a big help!

That is not how the built-in speed variable works. The point of using it is to not update x and y manually, but use speed and direction instead.
Thanks alot! Your response helped me alot! :) I really appreciate the help.
 
  • Like
Reactions: Roa

Roa

Member
You need to assign the speed value to either x or y in the step event.

So, in the bullets step event, if you want the bullet to travel up the screen just assign speed to the y axis:

y -= speed;
that would literally do nothing, as if you are not already traveling in room space, you have no speed to add to Y so it would just sit there.

Direction is the vector in 2d space, and speed is the magnitude.
 
Top