1up Indie
Member
GM Version: GMS2
Target Platform: All
Summary:
This video tutorial shows you how to make a laser beam in gamemaker studio for all directions. You will get an insight how particles systems work and how to utilize lengthdir to draw the laser on the screen towards the direction of the mouse position. The tutorial is very detailed so beginners and veterans can use (and hopefully understand) it alike.
Tutorial:
Object laser ->
Create:
Step:
draw:
and how the player can fire that thing, objPlayer -->
create:
step:
Target Platform: All
Summary:
This video tutorial shows you how to make a laser beam in gamemaker studio for all directions. You will get an insight how particles systems work and how to utilize lengthdir to draw the laser on the screen towards the direction of the mouse position. The tutorial is very detailed so beginners and veterans can use (and hopefully understand) it alike.
Tutorial:
Object laser ->
Create:
Code:
xEnd = 0; // breakpoints
yEnd = 0;
length_laser = 0; // total length used to determine and draw laser
// change here to change color and glow type
color = 1; // 0 blue, 1 orange, 2 violet, 3 pink, 4 red, 5 green
whatImage = spr_Laser_Hard_glow_38px; // spr_Laser_Hard_glow_38px or spr_Laser_Soft_glow_38px or spr_Laser_No_glow_38px
// change here to ajust to your needs for "refresh" rate of laser collision check
// memory space saver, less checks per step
refresh_hit = false;
refresh_hit_time = 20; // 3 times per second
refresh_hit_timer = refresh_hit_time;
Step:
Code:
// grab start position and direction/angle
x = o_Player.x;
y = o_Player.y;
direction = point_direction(o_Player.x, o_Player.y, mouse_x, mouse_y );
#region // define how long the laser can get max plus when hitting something
var max_length = 900; // change if camera is bigger or smaller then 900
for (i = 0; i < max_length; i++ ) {
xEnd = x + lengthdir_x(i, direction);
yEnd = y + lengthdir_y(i, direction);
length_laser = i; // how long the laser is in the end
// breaking point change here object to break to or add multiple breaking points
if (collision_point(xEnd, yEnd, o_Wall,0,0 ) ) {
part_particles_create(o_Particle_Setup.particleSystem, xEnd, yEnd, o_Particle_Setup.particleType_Hit_Blue, 1 );
part_particles_create(o_Particle_Setup.particleSystem, xEnd, yEnd, o_Particle_Setup.particleType_Spark_Blue, 10 );
break; // stops loop and sets new xEnd and yEnd values
}
} // end of for loop
#endregion
#region collision -> with global enemy to change its values (hp or something)
if (instance_exists(o_enemy)) {
// add
if (refresh_hit == true) { refresh_hit = false;
var _list = ds_list_create();
var hits = collision_line_list(x,y, xEnd, yEnd, o_enemy, 0,0, _list,0 );
if (hits > 0 ) {
for (var k = 0; k < hits; ++k;) {
_list[| k].hp = _list[| k].hp -1;
}
}
ds_list_destroy(_list);
} // end of refresh check
} // end of enemy exist check
//Memory save , "switch"
if (refresh_hit == false) {
refresh_hit_timer--;
if (refresh_hit_timer <= 0) { refresh_hit_timer = refresh_hit_time; refresh_hit = true; }
}
#endregion
draw:
Code:
for (j = 0; j < length_laser; j++ ) {
var flicker = random_range(0.6,1.4); // change to make diffrent flicker, or delete flicker and set y_scale back to 1
// variables set in create to ajust to need!
draw_sprite_ext(whatImage, 0, x + lengthdir_x(j, direction), y + lengthdir_y(j, direction), 1, flicker, direction,c_white, 1 );
}
//draw_line(x,y, xEnd, yEnd); not need, just good to check for collisions
and how the player can fire that thing, objPlayer -->
create:
Code:
pressed_Mouse = false;
Code:
// laser beam create
if mouse_check_button(mb_left) {
if !(instance_exists(o_Laser__Green)) {
instance_create_layer(mouse_x , mouse_y, "Instances",o_Laser__Green ); } }
//laser beam kill if no input
if !mouse_check_button(mb_left) { instance_destroy(o_Laser__Green); }
Last edited: