GMS 2 [Solved] Shot 2 bullets at the right and the left of a player in movement.

VMX

Member
Hi guys,

I'm a beginner in game maker and I have my first real issue. I will try to explain it as simple as possible :
I want a tourret to shot 2 bullets (one at the right and the other at the left of my player object). Those 2 bullets will be connected with an electric line.
But I don't what direction I have to give in the bullet create event because the player object can move.
Someone told me to try with lenghtdir, I also read things about dsin and dcos but I still have no idea to create that.

This is the code I made to create an simple bullet :


Create event of my turret :
Code:
if(irandom(100)==1)
{
    instance_create_layer(x , y , "Instances", obj_bullet);
}

image_angle = point_direction(x,y,obj_player.x,obj_player.y)
}

Step event of my bullet :
Code:
speed = 9;

direction = point_direction(x,y,obj_player.x,obj_player.y)
image_angle = point_direction(x,y,obj_player.x,obj_player.y)
Can someone explain me how to do it correctly ?
Thanks in advance.
VMX
 

TheouAegis

Member
For clarification, do you mean shooting something like a bolas?
edit: fixed typo. thanks
Code:
var dir = point_direction(x,y,obj_player.x,obj_player.y);
var barrel = 24; //this is the distance from the turret's origin to the tip of the barrel
var spread = 16;  //this is HALF the distance between both bullets
var x1 = x + lengthdir_x(barrel,dir);
var y1 = y + lengthdir_y(barrel,dir);
var a = lengthdir_x(spread,dir+90);
var b = lengthdir_y(spread,dir+90);
var x2 = x1 + a;
var x3 = x1 - a;
var y2 = y1 + a;
var y3 = y1 - a;
with instance_create(x2,y2,obj_bullet) {
    direction = dir;
    image_angle = direction;
    link = instance_create(x3,y3,obj_bullet);
    with link {
        direction = dir;
        image_angle = direction;
        link = other.id;
    }
}
Both bullets will have a variable link which is the id of the other bullet linked to it. You can use these ids to draw the electric tether.
 
Last edited:
Imagine your turret is a circle, and the Sprite origin is the center. So if you wanted to fire a single shot from the center you would just create the ammo object at the turrets x and y. Now if you wanted to fire the shot from the left instead you would use lengthdir to figure out how far to place it. So,instead of just creating the ammo at x you would do x+lengthdir_x(16,180); and that would put the ammo 16 pixels from the center at 180 degrees which would be to the left. You would need to do this for y too if you wanted the ammo origin to rotate around the turret.

Look up the docs for lengthdir it explains things clearly.
 
  • Like
Reactions: VMX

VMX

Member
For clarification, do you mean shooting something like a bolas?
Yes, it's what I was thinking about.
As TailBit said, I suppose that :
Code:
var b = lengthdir_x(spread,dir+90);
should be lengthdir_y.

I understand the code now but when I run it I have this code error : Variable obj_bullet.y3(100041, -2147483648) not set before reading it. Whereas y3 is set in the code. Do you have any explication ?
 

TailBit

Member
Put "var" in front of the 4 lines before the "with".

With makes the bullet perform the code, an alternative is to refer back to the turret object with other.y3 inside the with.. But using var is fine here
 

VMX

Member
Put "var" in front of the 4 lines before the "with".

With makes the bullet perform the code, an alternative is to refer back to the turret object with other.y3 inside the with.. But using var is fine here

With a few modifications, it's finally working thank you !
 
Top