Returns the horizontal x-component of the vector determined by the indicated length and direction.


GML Code Example

lengthdir_x(len, dir);

Argument Description
len The length away of the point to return.
dir The direction of the point to return.

Returns: Real


This function seems confusing at first, but it's not really. It is used to get the x component of a position "len" pixels from the starting point and in direction "dir". Imagine a circle around your instance, then imagine a point anywhere on that circle. To move to that point we need to move the object so many pixels in that direction... so this function (when used with lengthdir_y) gets the position of that point on the circle to be used in code by the instance. See the image below for details:


GML Code Example

instance_create(x + lengthdir_x(64, image_angle), y + lengthdir_y(64, image_angle), obj_bullet);

This will create a bullet instance at (x,y), but with 64 pixels added in the direction of image_angle. This is typically used for syncing a bullets start position with the end of a gun in game.

These examples are submitted by the community. Upvote the examples you think are most helpful.

3D Text by /u/JaxDrumm on December 04, 2016

GML Code

for(i=0; i < 25; i += 1) //Run the following code 25 times in one frame
		draw_set_color(merge_colour(c_black,c_red,i/25)) //Set the proper color based on the variable 'i'.
		draw_text(x+lengthdir_x(i,270),y+lengthdir_y(i,270),"3D Text."); //Draw in the proper place based on the variable 'i'.

Edited On December 04, 2016

No Edits Suggested

Is this page unclear or not descriptive enough? Suggest an edit to the page and with enough upvotes your changes will be made.

No Questions Have Been Ask

Do you have a question about this page? Ask it Here