• 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 Spawning an instance relative to the rotation of a different instance?

S

SB-338

Guest
I have an asteroids like game. I am trying to make a power up which replaces the normal rate of fire with 3 bullets which spawn next to each other. It works, but only when facing straight up and down. See video for example.

The idea is that the 3 bullets will be in a kind of wall shape in whatever direction the player is facing, but they are only like that when fired directly up and down, the two side bullets don't maintain the orientation of the ship so when fired in other directions they don't work correctly.

I could just make a new sprite of all three bullets in a single sprite and use precise mask, but then if only one bullet hit a target the whole instance would be destroyed effectively making it just one big bullet instead of three individual ones.

Here is the code in question which spawns the three bullets:

switch(global.powerup_type){

case "Multi-Shot":
if(canshoot == true){
canshoot = false;
alarm_set(0,20);
var pew1 = instance_create_layer(x,y,"Main",obj_bullet);
pew1.direction = image_angle;
pew1.image_angle = image_angle;
var pew = instance_create_layer(pew1.x+20,pew1.y,"Main",obj_bullet);
pew.direction = image_angle;
pew.image_angle = image_angle;
pew.x = pew1.x + 20;
pew.y = pew1.y;
var pew = instance_create_layer(pew1.x-20,pew1.y,"Main",obj_bullet);
pew.direction = image_angle;
pew.image_angle = image_angle;
pew.x = pew1.x - 20;
pew.y = pew1.y;
}


break;

and here is the code for the regular shooting (there is no issue transitioning to and from the shooting modes but I'm including it just in case.

default:
if(canshoot == true){
canshoot = false;
alarm_set(0,10);
var pew = instance_create_layer(x,y,"Main",obj_bullet);
pew.direction = image_angle;
pew.image_angle = image_angle;
}
break;

Anyone got any idea how to fix this?
 

FrostyCat

Redemption Seeker
Learn how to use lengthdir_x() and lengthdir_y().
GML:
var ldx = lengthdir_x(20, image_angle+90);
var ldy = lengthdir_y(20, image_angle+90);
var pew1 = instance_create_layer(x, y, "Main", obj_bullet);
...
var pew = instance_create_layer(pew1.x+ldx, pew1.y+ldy, "Main", obj_bullet);
...
var pew = instance_create_layer(pew1.x-ldx, pew1.y-ldy, "Main", obj_bullet);
...
 

Mk.2

Member
GML:
var len = 20;
var dir = image_angle;
var lx = lengthdir_x(len,dir+90);
var ly = lengthdir_y(len,dir+90);
pew = instance_create_layer(x+lx,y+ly,"Main",obj_bullet);

var lx = lengthdir_x(len,dir-90);
var ly = lengthdir_y(len,dir-90);
pew = instance_create_layer(x+lx,y+ly,"Main",obj_bullet);
 
S

SB-338

Guest
GML:
var len = 20;
var dir = image_angle;
var lx = lengthdir_x(len,dir+90);
var ly = lengthdir_y(len,dir+90);
pew = instance_create_layer(x+lx,y+ly,"Main",obj_bullet);

var lx = lengthdir_x(len,dir-90);
var ly = lengthdir_y(len,dir-90);
pew = instance_create_layer(x+lx,y+ly,"Main",obj_bullet);
Thank You!! It Worked!
 
Top