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

GML Making a projectile shoot up,down,left and right

J

Jack Rankin

Guest
I am creating a top down RPG and one of the items which the user cna pick up is a bow, i am having a probelm so when my character faces up, down or left the arrow does not go the direction where the character is facing and just goes right. Any help would be great! The button to shoot the arrow (when the bow is picked up) is the left click on the mouse. The version of gamemaker is 8.1.

if the user presses the A key: direction = left
if the user presses the D key: direction = right
if the user presses the S key: direction = down
if the user presses the A key: direction = up
the projectile = obj_Arrow, obj_Arrowleft, obj_Arrowup, obj_Arrowdown
speed = 5;
 
B

brupibo

Guest
Idk GM8.1 but the logic is probably the same: change the x and y speed of the arrow on its creation.

Code:
arrow = instance_create(x, y, obj_Arrow)
If direction == right {
arrow.hspeed = 1;
arrow.image_angle = 0; // considering your sprite arrow is facing right
}
if direction == left {
arrow.hspeed = -1;
arrow.image_angle = 180;
}
if direction == up {
arrow.vspeed = -1;
arrow.image_angle = 90;
}
if direction == down {
arrow.vspeed = 1;
arrow.image_angle = 270;
}
 

TheouAegis

Member
As brupibo showed, you don't need 4 different arrow objects, just one.

Additionally, if you used 4 arrow objects in spite of brupibo's suggestion, then you should be creating the correct arrow already with something like

if direction = left instance_create(x,y,obj_LeftArrow);
else if direction = right instance_create(x,y,obj_Arrow);
else if direction = up instance_create(x,y,obj_UpArrow);
else if direction = down instance_create(x,y,obj_DownArrow);

In which case each of your arrows should have their own code that makes them move in the correct direction. If you have 4 arrow objects, then there is no good reason that all four of them would be moving the same direction.
 
J

Jack Rankin

Guest
Idk GM8.1 but the logic is probably the same: change the x and y speed of the arrow on its creation.

Code:
arrow = instance_create(x, y, obj_Arrow)
If direction == right {
arrow.hspeed = 1;
arrow.image_angle = 0; // considering your sprite arrow is facing right
}
if direction == left {
arrow.hspeed = -1;
arrow.image_angle = 180;
}
if direction == up {
arrow.vspeed = -1;
arrow.image_angle = 90;
}
if direction == down {
arrow.vspeed = 1;
arrow.image_angle = 270;
}
Yeah, thanks very much figuerd it out myself and this is excatly what i done ty.
 
Top