GameMaker How do I make bullets spawn from the end of the gun in an 8-directional game?

C

CJacobsSA

Guest
Hello. I'm making my first game with 8-direction controls and I'm trying to decide on a method of storing, calling, and using offsets for gun barrel position. I am aware of how to use lengthdir or manually specify an offset in a sidescroller or a top-down shooter for example, but there's a twist here that's making me scratch my head.The game is isometric, not top-down, so the gun's barrel is a different X and Y distance from the player's origin for each angle. For example:





The player moves in 8-directions but the gun aims in 16-directions for smoothness' sake. Each aim angle will need two integers to determine where (from the player's origin) the bullet should spawn at. This means, at minimum, 32 values for X and Y, repeated for every weapon in my game. My question is, what is the "best" way to store so many variables and recall them when a bullet is fired? And is there a simpler way to go about this?
 

Tony Brice

Member
Code:
if (keyboard_check_pressed(vk_space)) {
   var bulletOffsetX = lengthdir_x(16,image_angle);
   var bulletOffsetY = lengthdir_y(16,image_angle);
   var newBullet = instance_create_layer(x+bulletOffsetX,y+bulletOffsetY,"Instances",objPlayerMissile);
   with (newBullet) {
       motion_add(objPlayer.image_angle,PLAYER_MAX_SPEED+2);
   }
}
This is how i've done it for a game that works in a similar way and you can drop it into your step event for your player object. You'll need to change the 16 to whatever your sprite height/width is for your player sprite and divide it by 2. The rest of it is pretty self-explanatory.
 
C

CJacobsSA

Guest
Doesn't seem to work unfortunately. The bullets just spawn slightly to the right of the player instead of in the middle of them now.
 

curato

Member
what he is saying is look at the origin of your sprite then look at where you would want the bullet to come out the barrel. Get the x and y difference in position that is your offset.
 

Tony Brice

Member
what he is saying is look at the origin of your sprite then look at where you would want the bullet to come out the barrel. Get the x and y difference in position that is your offset.
Exactly this. I haven't broken it down into cut and paste code. Just given you something you can build on.
 
Top