Cursor Trails in GameMaker Studio 2

GMS2 "Cursor Trails"

Here is a quick cursor trail effect for your GameMaker Studio 2 game!

Here’s an example of the effect we will be creating:

As we can see, the spear and the cursor have a great effect that makes them stand out.

Create Event

// Maximum number of line points
trail_steps = 10;

// Initialize lists for mouse positions (x, y)
trail_x = ds_list_create();
trail_y = ds_list_create();

Step Event

// Insert current mouse postion in the list
ds_list_insert(trail_x, 0, mouse_x);
ds_list_insert(trail_y, 0, mouse_y);

// Remove the last point if exceed the maximum points limit
if ds_list_size(trail_x) >= trail_steps
{
ds_list_delete(trail_x, ds_list_size(trail_x) – 1);
ds_list_delete(trail_y, ds_list_size(trail_y) – 1);
}

 

(if you change mouse_x,mouse_y to another point it will follow that point)

Draw Event

// Draw current sprite
draw_self();

// Init a primitive drawing
draw_primitive_begin(pr_trianglestrip);

// Stores the size of list
var l_size = ds_list_size(trail_x);

// List iteration
for (var i = 0; i < l_size; i++)
{
// Current position
var x1 = ds_list_find_value(trail_x, i);
var y1 = ds_list_find_value(trail_y, i);

// Next position
var x2 = ds_list_find_value(trail_x, min(i + 1, l_size – 1));
var y2 = ds_list_find_value(trail_y, min(i + 1, l_size – 1));

// Set the line ‘thickness’
var rr = 2 – ((i / l_size) * 2);

// Direction
var dd = point_direction(x1, y1, x2, y2);

// Opacity
var aa = 1 – ((i / l_size));

// Color
var cc = make_color_hsv((current_time * 0.5) mod 255, 255, 255); // or use c_white

// Draw first vertice
draw_vertex_color( x1 + lengthdir_x(rr, dd + 90),
y1 + lengthdir_y(rr, dd + 90),
cc, aa);

// Draw second vertice only if is not the fisrt or last point
if i != 0 or i != l_size-1
draw_vertex_color( x1 + lengthdir_x(rr, dd – 90),
y1 + lengthdir_y(rr, dd – 90),
cc, aa);
}

// Finishes primitive drawing
draw_primitive_end();

How it Works

Download Example – https://alex-fc.itch.io/gms2-mouse-trail-effect

More Posts

Send Us A Message

Cursor Trails in GameMaker Studio 2

More Posts

Send Us A Message