• 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!

Laser Beam Troubles

I've been following a lot of tutorials on how to make laser beams but one thing I can't figure out is how to make one that starts at the player's ship and grows outward until it reaches a certain length and then stops unless it comes into collision with an asteroid. If it collides with an asteroid I want it to add to the player's inventory for Silicate. Any examples of how I could achieve this? Any help would be greatly appreciated.
 
A lot depends on the effect you want to create, but if you know the direction from which your laser is starting, you could do something like this:

GML:
for (i = 0; i <= laser_max_distance; i++){//could also do i+2 etc. to save on performance, this would skip spaces keep in mind
    if (instance_position(laser_start_x + lengthdir_x(i, laser_direction), laser_start_y + lengthdir_y(i, laser_direction), object_asteroid)){
        laser_end_x = laser_start_x + lengthdir_x(i, laser_direction);
        laser_end_y = laser_start_y + lengthdir_y(i, laser_direction);
        //do stuff here to collect resources, destroy asteroid, etc.
        break;
    }
}

//then in the draw event
draw_line_color(laser_start_x, laser_start_y, laser_end_x, laser_end_y, laser_color, laser_color);
Keep in mind this might draw a laser all the time, so you may want to set parameters for when to draw the laser, such as making some variable true when you press the fire button, and then only drawing when that variable is true.

Also use your own variable naming conventions of course, and remember to initialize variables in the create event.
 
Top