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

Setting a bullet's direction

TheOnlyWRT

Member
I am trying to use just one bullet object in order to conserve the use of objects. In the step event of the different enemies when they shoot, I assumed that you could set the direction of the bullet in the step event, but that just changes the bullets direction every time the enemy changes direction. How can I set the bullets direction in the step event when it is created so that i can only use one bullet object?

Thanks!
 

Simon Gust

Member
when creating the bullet you can set the direction at the start
Code:
var bullet = instance_create(x, y, obj_bullet);
bullet.direction = point_direction(x, y, mouse_x, mouse_y);
This will set direction for just the new bullet instance.
 

TheOnlyWRT

Member
when creating the bullet you can set the direction at the start
Code:
var bullet = instance_create(x, y, obj_bullet);
bullet.direction = point_direction(x, y, mouse_x, mouse_y);
This will set direction for just the new bullet instance.
but i dont want to set it to the mouse direction.......... i want to set it to left or right.........
 

TheOnlyWRT

Member
Here is the code that I have right now:

BULLET CREATE EVENT
Code:
//variables for the bullet

dir = 0;
velocity = 20;
hsp = dir*velocity;
stability = 0;
damage = 0;
BULLET STEP EVENT
Code:
//movement
x = x + hsp;
ENEMY STEP EVENT
Code:
if((alarm[0] < 0)){
            
            attacking = true;
            alarm[0] = weapon[currentWeapon, v_reloadSpeed]; // Set the shoot alarm
            shotsFired += 1;
            var _inst = instance_create_depth((x + sign(hsp)*20), y-7, 1, objectBulletEnemy);
            _inst.damage = weapon[currentWeapon, v_damage]; // Set bullet damage
            _inst.dir = point_direction(x, y, x+dir, y);
            //set the stability
            _inst.stability = weapon[currentWeapon, v_stability] //set the stability
            
        }
I hope that this will help you find out how i could program this better. Thank you!!!!
 

TheOnlyWRT

Member
Why use point_direction if dir already gives you the direction? Just do inst.dir=dir
because the bullet direction changes every time the enemy changes direction....... like, i remember that when i used D&D, i would just set the direction in the create event and the direction would never change. Now that I am setting the bullets direction in the step event, the direction changes. How can set it so that the bullet continues in one direction even if the player changes direction?
 

sp202

Member
Well, point_direction isn't preventing that, so swap it out. I can't see how your bullet's direction can keep changing when it's only assigned a direction upon creation.
 
Let's see.

You are updating you bullets x position using "hsp". However, you are only calculating "hsp" in the bullets Create Event.

The bullets Create Event runs the very moment you create it.

Then, you are updating the bullets "dir" variable, but as you are moving the bullet via "hsp" this will do nothing, because hsp remains as it was set in the Create Event.

Therefore, all your bullets are moving according to "hsp", which is always going to be dir * velocity as per your Create Event, your "dir" == 0.

So all bullets are going to be [shooting to the right.] EDIT: Cancel that - they won't be moving at all.

It should be more like this:

Bullet Create Event
Code:
dir = 0
velocity = 20
hsp = 0
...rest of code
Enemy Step Event (where you create the bullet)
Code:
var _inst = instance_create_depth((x + sign(hsp)*20), y-7, 1, objectBulletEnemy);
_inst.direction = dir;
_inst.speed = _inst.velocity;
Then you can delete the bullets Step Event and let Game Maker handle the movement of the bullet, because you have set the built-in variables "speed" and "direction", which Game Maker will use to move the bullet automatically.

If you must use hsp, then you will want to do this:

Enemy Step Event
Code:
var _inst = instance_create_depth((x + sign(hsp)*20), y-7, 1, objectBulletEnemy);
_inst.hsp = dir * inst.velocity;
...and leave the Enemy Bullet as "x += hsp"
 
Last edited:
T

TonyCold

Guest
I'll make it brief for you. If you are trying to make the bullet move Left and Right then you can use that code:
Code:
if (image_xscale (of player) = 1 && (shoot (Your key that shoots))){
    image_xscale(of bullet) = 1 
    x += spd
else if (image_xscale (of player) = -1 && (shoot)){
    image_xscale(of bullet) = - 1
    x -= spd
}
}
 

sp202

Member
@TonyCold How does that code make any practical sense? The bullet only moves when shoot is held down? Turning the player around makes the bullet do the same?
 
T

TonyCold

Guest
@sp202 First of all this is not the whole Code but just a heads up. And Yeah. If the player is facing the Right direction and then Presses the Shoot button the Bullet will face the Right direction also and It will go The whole way right. If the player is facing the Left direction the Bullet will face the left direction also and If he presses the Shooting button it will Move the whole way left (Until if collides with something.). Briefly, obj_player.image_xscale = obj_bullet.image_xscale. That's how it works.
 

sp202

Member
@TonyCold First of all your bracket was in the wrong place, so I'll assume you meant to have a working else if statement and put it before the else. Depending on whether this code is in the create event or step event it'll work partially but it's still useless in either.

In the create event, assuming you aren't quick enough to release the key before the bullet is spawned, its image_scale will be set to the player's image_scale and it will move once and never again.

In the step event, the bullet will move as long as you've held down the shoot key, let go and it'll freeze. Change the direction the player is facing and suddenly the bullet is travelling right back at the player (provided you're holding down shoot).

That's how it works.
 

TheouAegis

Member
Post the complete codes for your enemy and for your bullet. Because everything we've said does work -- we've all used it. Show us what you have now and we'll tell you where you messed up.
 

TheOnlyWRT

Member
got it to work, i just put an if statement in the create code and now it sets the direction based on the players location. Thank you all for your help
 
O

Obj_Neil

Guest
In the bullet create event, create a variable facing = o_player.facing and then in step event, x += 3 * facing. This way the - or positive from multiplying will only happen upon the bullets creation. Just in case that is not how you fixed it, that is an easy way. -- You can also set it to enemy image_scale by creation a variable for that in enemy create.
 
Top