• 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*Shoot a bullet off center from player

K

Kamon145

Guest
Hi all, I am working on a space shooter and would like to have the bullets come out of the side of the ship, and alternate between left side and right side. As in, the ship can rotate 360 degrees and i want my bullet objects spawning at the tip of one wing, than from the other. Ive used lengthdir_x, and y before but this just seems to spawn the bullet out further from the center, not really moving it from left or right. I came up with one way to do this but it just seems hacky and is causing some other problems. I took my bullet object and had the Y origin at -16, than every time we shoot, a variable called flip is switched from 1, -1 and the bullets image yscale is set to that variable on creation. This does give me the effect I'm looking for with them alternating sides but gets weird if i try to turn the bullets at all because of their way off center origin. So does anybody have a cleaner way of doing this without having a wayyy off center origin?
 

Slyddar

Member
lengthdir_x and y would be the answer, but use your flip variable to change the length from positive to negative. The direction can remain the same if it's being spawned from the center of your ship, as that would make it happen along the same vector, but on opposite sides of the ship.
 
K

Kamon145

Guest
lengthdir_x and y would be the answer, but use your flip variable to change the length from positive to negative. The direction can remain the same if it's being spawned from the center of your ship, as that would make it happen along the same vector, but on opposite sides of the ship.
Thanks for the reply, unfortunately when I set the lengthdir_x to anything different than the lengthdir_y, as the ship rotates the bullets are created further or closer to the ship,
I am using them like this :
Code:
var xoffset = lengthdir_x(8,aim_dir);
var yoffset = lengthdir_y(8,aim_dir);
instance_create_layer(x+xoffset,y+yoffset,"Instances",obj_bullet);
but yeah having the x at 16 causes weird results when rotating, aim_dir is linked to image_angle so its whatever way the sprite is rotated
 

johnwo

Member
Assuming the sprite origin is at sprite_width/2, sprite_height/2, and tip of guns are at originX+8 and originY±8 just use a variable to negate the Y-coordinate.

Code:
/// Create event
_side = 1; // The side of which the bullet will spawn


/// When shooting is triggered
_side = -_side; // Negate the value to alternate the bullet-spawn point

var bulletSpawnX = 8; // Set the X coordinate
var bulletSpawnY = 8*_side; // Set the Y coordinate, multiplying with _side to alternate the side of which the bullet will spawn

_length = point_distance(0, 0, bulletSpawnX, bulletSpawnY);
_angle = point_direction(0, 0, bulletSpawnX, bulletSpawnY);
_bulletX = lengthdir_x(_length,_angle+image_angle);
_bulletY = lengthdir_y(_length,_angle+image_angle);

instance_create(x + _bulletX,y + _bulletY, obj_bullet);
 
K

Kamon145

Guest
Assuming the sprite origin is at sprite_width/2, sprite_height/2, and tip of guns are at originX+8 and originY±8 just use a variable to negate the Y-coordinate.

Code:
/// Create event
_side = 1; // The side of which the bullet will spawn


/// When shooting is triggered
_side = -_side; // Negate the value to alternate the bullet-spawn point

var bulletSpawnX = 8; // Set the X coordinate
var bulletSpawnY = 8*_side; // Set the Y coordinate, multiplying with _side to alternate the side of which the bullet will spawn

_length = point_distance(0, 0, bulletSpawnX, bulletSpawnY);
_angle = point_direction(0, 0, bulletSpawnX, bulletSpawnY);
_bulletX = lengthdir_x(_length,_angle+image_angle);
_bulletY = lengthdir_y(_length,_angle+image_angle);

instance_create(x + _bulletX,y + _bulletY, obj_bullet);
This almost works! when the player is facing left or right the bullets show up right where they are supposed to, but when facing up or down, they are both created on the right, one 8 pixels above the other, so somehow its not rotating right with the player still, i tried multiplying bulletSpawnY*_side as well and that makes them shoot out of the right side no matter what direction we are facing, but they are also offset by about 8 pixels, meaning one is created further away from the gun, while the other side is created further back into it, is there something else that i should be flipping ?

**EDIT** I got it! Lol I forgot the image angle is controlling the body of the ship... and I was using a different variable called aim_dir to control direction of guns... so yeah changed image angle to aim_dir and now it works perfectly, thanks so much for the help!
 
Last edited by a moderator:

johnwo

Member
Kinda hard to tell w/o seeing your code. Kinda ends up at guesswork...
For example: If you're using image_xscale or similar, just multiply _bulletX by that as well.
 
K

Kamon145

Guest
Kinda hard to tell w/o seeing your code. Kinda ends up at guesswork...
For example: If you're using image_xscale or similar, just multiply _bulletX by that as well.
Nah you got it right the first time man, I just wasn't using the right variable :p
 
Top