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

SOLVED Bullet not firing from correct image angle

allups

Member
Hello Everyone, I'm having issues with my simple shooting script. in this script, I used the Gamemaker Astroid script as a reference for the shooting, the rest is just Shaun Spalding's movement and wall jump with variable jump implementation.

How it's supposed to work; The projectile shoots out the left and right of the player object depending on what way the player is facing, and if the player is on a wall and not on the ground shoot away from the wall the player is on.

how it works now; the projectile shoots regardless of the player state and only shoots from the right side of the player object.

I'm on gamemaker v2.3.7.606

Player step// the Shooting bit of the code
Code:
key_left = keyboard_check(ord("A"));
key_right = keyboard_check(ord("D"));
key_jump = keyboard_check_pressed(vk_space);
key_jump_held = keyboard_check(vk_space);
key_shoot = mouse_check_button_pressed(mb_left)

//shooting
if(key_shoot)
{

    var inst = instance_create_layer(x,y,"Bullets", OBullet);        
        inst.direction = image_angle;
        inst.image_angle = image_angle;
}
Bullet create
Code:
speed = 6;
Bullet post draw
Code:
if (place_meeting(x,y,OWall)) instance_destroy(); // when the bullet hits a wall kill bullet
I tried to be as clear as possible. This is also my first post to the forums so sorry if I don't have all the etiquette down.
 

Bearman_18

Fruit Stand Deadbeat
It shoots to the right because the "image_angle" of the player is always 0, unless you change it. I'm not familiar with the tutorial you mentioned, but the direction the player is facing is usually based on "image_xscale." Does Shaun's movement code use that anywhere?
 

allups

Member
It shoots to the right because the "image_angle" of the player is always 0, unless you change it. I'm not familiar with the tutorial you mentioned, but the direction the player is facing is usually based on "image_xscale." Does Shaun's movement code use that anywhere?
Yes, it does. in the adjust sprite section. and my player does turn left and right during gameplay, is that not determined by an image_angle?. and here is the rest of the Player set script. I just didn't wanna bloat the post/add useless info.

Code:
key_left = keyboard_check(ord("A"));
key_right = keyboard_check(ord("D"));
key_jump = keyboard_check_pressed(vk_space);
key_jump_held = keyboard_check(vk_space);
key_shoot = mouse_check_button_pressed(mb_left)

//shooting
key_left = keyboard_check(ord("A"));
key_right = keyboard_check(ord("D"));
key_jump = keyboard_check_pressed(vk_space);
key_jump_held = keyboard_check(vk_space);
key_shoot = mouse_check_button_pressed(mb_left)

//shooting
if(key_shoot)
{

    var inst = instance_create_layer(x,y,"Bullets", OBullet)    
        inst.direction = image_xscale; //changed from image_angle to image_xscale
        
}

//wall jump
if (onwall != 0) && (key_jump) && (!onground)
{

    walljumpdelay = walljumpdelay_max;
    
    hsp = -onwall * hsp_wjump;
    vsp = vsp_wjump;
    
    hsp_frac = 0;
    vsp_frac = 0;
}

//calc horizontal movemnet
walljumpdelay = max(walljumpdelay-1,0);
if (walljumpdelay == 0)
{
    var dir = key_right - key_left;
    hsp += dir * hsp_acc;
    if(dir == 0)
    {
        var hsp_fric_final = hsp_fric_ground;
        if (!onground) hsp_fric_final = hsp_fric_air;
        hsp = lerp(hsp,0,hsp_fric_final);
    }
    hsp = clamp(hsp,-hsp_walk,hsp_walk);
}



//cal vertical movment
var grv_final = grv;
var vsp_max_final = vsp_max;
if (onwall != 0) && (vsp > 1)
{
    grv_final = grv_wall;
    vsp_max_final = vsp_max_wall;

}
vsp += grv_final;
vsp = clamp(vsp,-vsp_max_final,vsp_max_final);

//ground jump
if (jumpbuffer > 0)
{    jumpbuffer--;
    if (key_jump)
    {
        jumpbuffer = 0;
        vsp = vsp_jump;
        vsp_frac = 0;
        jumped = true;
    }
}

//variable jump
if(jumped) and (key_jump_held) and (vsp < 0)
{
    grv = jump_grv
}
else grv = norm_grv;
vsp = clamp(vsp,-vsp_max,vsp_max);

//Dump fraction and final ints
hsp += hsp_frac;
vsp += vsp_frac;
hsp_frac = frac(hsp);
vsp_frac = frac(vsp);
hsp -= hsp_frac;
vsp -= vsp_frac;

//horizontal collison
if (place_meeting(x+hsp,y,OWall))
{
    var onepixel = sign(hsp);
    while (!place_meeting(x+onepixel,y,OWall)) x += onepixel;
    hsp = 0;
    hsp_frac = 0;

}

//horizontal move
x += hsp;

//vertical collision 
if (place_meeting(x,y+vsp,OWall))
{
    var onepixel = sign(vsp);
    while (!place_meeting(x,y+onepixel,OWall)) y += onepixel;
    vsp = 0;
    vsp_frac = 0;
}
//Vertical move
y += vsp;  

//Calc current status
onground = place_meeting(x,y+1,OWall);
onwall = place_meeting(x+1,y,OWall) - place_meeting(x-1,y,OWall);
if (onground) jumpbuffer = 6;

if (onground) jumped = false;

//adjust sprite
image_speed = 1;
if (hsp != 0 ) image_xscale = sign(hsp);
if (!onground)
{
    if (onwall !=0)
    {
        sprite_index = SplayerWall;
        image_xscale = onwall;
    }
    else
    {
    
    dust = 0;
    sprite_index =     SplayerAir;
    image_speed = 0;
    image_index = (vsp > 0);
    }
}
else
{
    if (hsp != 0) sprite_index = SplayerMov; else sprite_index = Splayer;
}
 
is that not determined by an image_angle?
It can be, but it's not in this case. image_xscale refers to how much the sprite is stretched or squashed in the x axis. A negative value "flips" the sprite. So while it might appear as though the "angle" of the sprite is changing, it's not. In fact, if you set image_angle to 180, you'll notice the sprite is actually upside down! This makes sense if you think of how a sprite would actually rotate. So image_angle is not being touched if you are changing image_xscale. You'll have to refer to image_xscale instead of image_angle.
 

allups

Member
try this:
GML:
var inst = instance_create_layer(x,y,"Bullets", OBullet)
inst.direction = image_xscale*180;
that did make the bullet come out the left side but it still isn't adjusting for what way the sprite is facing. I could make them dual wield by copy and pasting the code and removing the -180 but it doesn't feel optimal without taking into consideration what way the sprite is facing
 
I would just do a simple if else statement. It's the easiest to read and understand.
Code:
if (image_xscale >= 0) {
  inst.direction = 0;
}
else {
   inst.direction = 180;
}
 
Go to the very top of the forum post, go to more options or the three dots, then click edit thread, there should be a drop-down menu next to the title, click it and select "Solved" and click save.
 
Top