Character keeps shoot Right, HELP!

D

Daniel Cantu

Guest
Hello, I am new to using Game Maker Studio 2, and programming in general. So far, I looked up tutorials for making a plat former, and I was doing great until the shooting mechanics are off. My character keeps shooting right, even though I'm facing left. Here is all of my code for my Obj_Player, because I'm starting to think that I messed up badly. Thank you for reading my plea. :3

Player Input
key_left = keyboard_check(vk_left);
key_right = keyboard_check(vk_right);
key_jump = keyboard_check_pressed(vk_up);


//Calculate Movement
var move = key_right - key_left;

hsp = move * walksp;

vsp = vsp + grv;

if (place_meeting(x, y+1,Obj_Wall)) && (key_jump)
{
vsp = -7;
}

//Shooting Physics for Platformer(MEGAMAN)
if(keyboard_check_pressed(vk_space)){
with (instance_create_depth(x,y,0,obj_bullet))
speed = 10;
direction = Obj_Player.direction;
image_angle = direction;
}
//Horizontal Collision
if (place_meeting(x+hsp,y,Obj_Wall))
{
while (!place_meeting(x+sign(hsp),y,Obj_Wall))
{
x = x + sign(hsp);
}
hsp = 0;
}
x = x + hsp;

//Vertical Collision
if (place_meeting(x,y+vsp,Obj_Wall))
{
while (!place_meeting(x,y+sign(vsp),Obj_Wall))
{
y = y + sign(vsp);
}
vsp = 0;
}
y = y + vsp;

///Animation
if (!place_meeting(x,y+1,Obj_Wall))
{
sprite_index = Spr_Player_Jump
image_speed = 0;
if (sign(vsp) > 0) image_index = 1; else image_index = 0;
}
else
{
image_speed = 1;
if (hsp == 0)
{
sprite_index = Spr_Player_Idle;
}
else
{
sprite_index = Spr_Player_Moving
}
}

if (hsp != 0) image_xscale = sign(hsp);
 
C

CedSharp

Guest
To put it simply, you are creating an instance of a bullet with speed = 10, then you set the direction of the bullet to the direction of the player.
It looks fine... until you realize the direction of the player never changes.

See, you are not using the built-in vairables for movement, you are manually setting x and y. Because of that, the direction variable of your player is never changed.
You can update your code by adding this right after the hsp variables at the top of your code (or anywhere you want, as long as it's after the move variable is defined and before you check for shooting):

Code:
// Change the direction the player is facing based on movement
if(move < 0) direction = 180;
else if(move > 0) direction = 0;
By setting the direction, the bullet should start shooting in the right direction.
 
Top