Need help with a laser beam

I would draw a stretched sprite, instead of a line, because it will allow more control.

In this situation I would create a white, 1x2 pixel (1 wide, 2 tall) sprite for the totally opaque part of the beam.

And then a taller sprite for the partially transparent part of the beam. This second one would have a grayscalye gradient going from brightest in the center, to darkest at the top and bottom. Like the other sprite, this one is only 1 pixel wide.

you can get the distance between the origin and the end of the laser with

laser_length = point_distance(origin_x,origin_y,end_x,end_y).

you can get the direction in a simlar way

laser_direction = point_direction(origin_x,origin_y,end_x,end_y).

and then draw the sprites this way

 var laser_length = point_distance(origin_x,origin_y,end_x,end_y).  
 var laser_direction = point_direction(origin_x,origin_y,end_x,end_y).
 var laser_color = make_color_hvs(random(255),255,255);  //makes the laser a random color
 draw_set_blend_mode(bm_add);
 image_alpha = sin(counter++*0.1)*0.25+0.75;  //make beam pulsate

 draw_sprite_ext(laser_sprite1,0,origin_x,origin_y,laser_length,1,laser_direction,laser_color,image_alpha);
 draw_sprite_ext(laser_sprite2,0,origin_x,origin_y,laser_length,1,laser_direction,laser_color,image_alpha);

counter is used to make the beam pulsate. It is avariable, can be stored anywhere as long as you recognize variable scope. It increases by 1 each step. if you want the beam to pulsate faster, change the constant "0.1" to a higher value, if you want the beam to pulsate slower, change the constant to a lower value. "*0.25+0.75" Makes the beam pulsate between 0.25 and 1.0 alpha transparency.

If you want to adjust the laser thickness, change it's yscale.

One last thing, I can show you how to make the laser shoot in any direction and correctly terminate at the point of contact with whatever it is hitting. Let me know if you're interested.

/r/gamemaker Thread