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

Discussion in 'Programming' started by CJacobsSA, Jul 22, 2019.

  1. CJacobsSA

    CJacobsSA Member

    Joined:
    Aug 30, 2017
    Posts:
    96
    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:

    [​IMG]

    [​IMG]

    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?
     
  2. Tony Brice

    Tony Brice Member

    Joined:
    Feb 4, 2017
    Posts:
    67
    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.
     
    Toque likes this.
  3. CJacobsSA

    CJacobsSA Member

    Joined:
    Aug 30, 2017
    Posts:
    96
    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.
     
  4. curato

    curato Member

    Joined:
    Jun 30, 2016
    Posts:
    409
    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.
     
  5. Tony Brice

    Tony Brice Member

    Joined:
    Feb 4, 2017
    Posts:
    67
    Exactly this. I haven't broken it down into cut and paste code. Just given you something you can build on.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice