1. Hey! Guest! The 35th GMC Jam will take place between November 28th, 12:00 UTC - December 2nd, 12:00 UTC. Why not join in! Click here to find out more!
    Dismiss Notice

2D Turrets Firing Angle

Discussion in 'Programming' started by Bulldrome, Nov 9, 2019.

  1. Bulldrome

    Bulldrome Member

    Joined:
    May 31, 2019
    Posts:
    12
    I'm working on a 2D platformer and one of the simple hazards I want are turrets that will fire at simple 45 degree angles (8 cardinal directions). I have it working now but I feel I did it in the most sloppy way possible. I have separate objects for each turret angle but I also have separate objects for each bullet that the turret fires.

    So I have a bullet object that fires at a 45 degree angle, a bullet object for a 90 degree angle, a bullet object for 135 degree angle and so on. This works but I feel this could have been done in a lot more efficient way.

    This is the code I have on the Alarm[0] event on the turrets
    var inst = instance_create_layer(x,y,"Instances",obj_hblastleft);
    inst.direction = image_angle;
    alarm[0] = room_speed * 3;

    and the bullets have simple hsp = *, vsp = * to set their angle to travel
     
  2. Nocturne

    Nocturne Friendly Tyrant Forum Staff Admin

    Joined:
    Apr 13, 2016
    Posts:
    6,984
    Can't you just make all your sprites right facing (which is 0º in GameMaker) and then set their image angle to the direction of fire?
     
  3. Bulldrome

    Bulldrome Member

    Joined:
    May 31, 2019
    Posts:
    12
    The turrets only exist to spawn in the bullet objects which have their own specific horizontal and vertical speed. The image angle of the turret wouldn't effect the bullet object
     
  4. Nocturne

    Nocturne Friendly Tyrant Forum Staff Admin

    Joined:
    Apr 13, 2016
    Posts:
    6,984
    You posted this code:

    inst.direction = image_angle;

    So why can't you just do:

    inst.image_angle = image_angle;

    too?

    I mean, it's really that simple. Make the instances all face right then rotate their image angle to the direction of travel or shooting.
     
  5. the_dude_abides

    the_dude_abides Member

    Joined:
    Jun 23, 2016
    Posts:
    676
    Don't let Nocturnes admin tag fool you. They wrote the manual for Gamemaker Studio, and are an accomplished programmer with several items on the marketplace. Which you can see listed in their post. In fact all of the admins here are community users with plenty of coding experience.

    What they suggest might not fit with your code, but, and I want to be kind here, that's most likely because you're doing it wrong to begin with.

    However: Nocturne didn't mention though that setting 'speed' in the bullet means you don't need to do anything with the bullets horizontal / vertical speeds at all. As GMS sets that for you when it has 'direction' and 'speed' for an instance - which can both be specific values per instance. You're already doing this when setting 'direction', and 'speed' is no different to access through "inst.speed = whatever value"
     
  6. Bulldrome

    Bulldrome Member

    Joined:
    May 31, 2019
    Posts:
    12
    I didn't mean to come off as rude if thats what it seemed.
    I'll try changing the code to what Nocturne posted when I can.
    So if I make 1 universal turret object that faces right with that code, would I then have to set the image angle through the creation code for each instance I put the turret in a room?
    Also for the bullet objects, should I just give them a blank horizontal speed and a blank vertical speed and then tweak them in the creation code depending on the turrets angle or should I just set a base speed and when the turret gets rotated, the bullet will just fire at the direction accordingly?
     
  7. the_dude_abides

    the_dude_abides Member

    Joined:
    Jun 23, 2016
    Posts:
    676
    I wouldn't say you were being rude :) Just saying that Nocturne, and the other admins, know their stuff. People like to learn, and do things their own way as part of that process. But eventually you reach a point where more experienced users have a better solution, and you can't argue against the result. I've been there, and butted my head against that wall - all you'll get is a sore head....

    1) The bullet object doesn't need a horizontal or vertical speed. Once speed and direction are defined (which can be on a per instance basis) the calculations for hspeed / vspeed are done automatically by GMS.

    2) You only need one gun object, and then, yes - you set the angle each one fires at in the creation code.

    3) The create event of the bullet object doesn't need any values set for speed / direction. Because variables such as 'speed' / 'direction' etc are inbuilt variables they don't need to be defined in an object (like permanent variables would) for later access. They are automatically set to zero anyway. You can test this for yourself by creating a new object with neither set - draw the values for 'speed' + 'direction', and you'll see they exist already.

    The turret object will define them for the bullet, like this:

    Code:
    var inst = instance_create(x, y, whatever you've called bullet) // I use GMS 1.4, so won't comment on the equivalent function in GMS 2...?
    ]inst.speed = whatever;
    inst.direction = image_angle; // image_angle is a value from the turret 
    inst.image_angle = image_angle;
    
    It's essentially the same as your code. The only difference is that you seem to have misunderstood how instances of an object are not the same thing as the object itself. Every single instance of the bullet object can have different values for speed, direction and image angle, and still be the same object.
     
  8. Bulldrome

    Bulldrome Member

    Joined:
    May 31, 2019
    Posts:
    12
    So if I were to plug this code in, delete the speed on the bullet objects and then set the speed in the code posted above. It'd then fire the bullet out to the right (0º) and then I could just change the angle in the creation code after placing the turret in the room?
    Sorry if I'm asking too many questions, just want to make sure I've got this right.
    Asking for help doesn't mean much if you don't learn from it
     
  9. the_dude_abides

    the_dude_abides Member

    Joined:
    Jun 23, 2016
    Posts:
    676
    I'm sure you'll figure it out. Good luck!
     
  10. Bulldrome

    Bulldrome Member

    Joined:
    May 31, 2019
    Posts:
    12
    I'm trying the code now and the good news, its firing at the turrets angle! I tried changing its angle and it fires correctly.
    My problem now is its firing the bullets at a rapid speed causing them to a full on beam. I tried to adjust the speed but that just dictated how slow the beam comes out.
     

    Attached Files:

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