1. Hello Guest! It's with a heavy heart that we must announce the removal of the Legacy GMC Archive. If you wish to save anything from it, now's the time! Please see this topic for more information.
    Dismiss Notice

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

Discussion in 'Programming' started by VMX, Dec 2, 2019.

  1. VMX

    VMX Member

    Joined:
    Dec 2, 2019
    Posts:
    3
    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
     
  2. TheouAegis

    TheouAegis Member

    Joined:
    Jul 3, 2016
    Posts:
    7,136
    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: Dec 3, 2019
  3. robproctor83

    robproctor83 Member

    Joined:
    Sep 30, 2019
    Posts:
    117
    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.
     
    VMX likes this.
  4. TailBit

    TailBit Member

    Joined:
    Oct 16, 2019
    Posts:
    76
    @TheouAegis
    Code:
    var b = lengthdir_x(spread,dir+90);.
    Should be lengthdir_y I guess.
     
  5. VMX

    VMX Member

    Joined:
    Dec 2, 2019
    Posts:
    3
    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 ?
     
  6. TailBit

    TailBit Member

    Joined:
    Oct 16, 2019
    Posts:
    76
    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
     
  7. VMX

    VMX Member

    Joined:
    Dec 2, 2019
    Posts:
    3

    With a few modifications, it's finally working thank you !
     
  8. TheouAegis

    TheouAegis Member

    Joined:
    Jul 3, 2016
    Posts:
    7,136
    Writing code out on my phone is so tedious...

    Thanks for catching all the typos. lol
     

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