+1 vote

Given initial and final Vector2( ) position, How can a dotted / dashed line be drawn ?

in Engine by (79 points)

5 Answers

+1 vote

Unfortunately, there is no built-in function for that in Godot yet. You must implement it yourself.

Here is one example I found which might helps you to start with it:

by (154 points)

There is a built-in way to achieve this with Godot using the Line2D node, but you need to supply your own dashed/dotted line texture.

+1 vote

You can use draw_multiline() and save positions from a point to another.

You can see how it works in this video:
In the video the dotted line follows a sine.

min: 13:20

by (404 points)
edited by
+1 vote

In 3.4.2+, you can use Line2D (add at least two points) and then specify a Texture and set 'Texture Mode' to 'Tile'. Super easy and works really well. You can even drag the ends around in your editor.

As for what to use for the texture, here's a an example: Make a png that is 16x4. Make it gray on the left half and then transparent on the right. Save and reference this as your Texture. Make sure to import your texture with Repeat. You can then play with other settings like Width and Self_Modulate.

Here's a gdscript example if you want to do it from code instead. node1 and node2 are two existing objects in the scene that I want to draw a dashed line between.

var line = Line2D.new()
line.texture = load("res://assets/map/dotted-line.png")
line.texture_mode = Line2D.LINE_TEXTURE_TILE
var poolVectorArray : PoolVector2Array = []
line.points = poolVectorArray

P.S. If you want to do a dotted line, just change your texture so half is a filled circle and the other half is transparent.

by (44 points)
edited by

Make sure to import your texture with Repeat enabled for it to work.

@Poobslag Ah, updated the verbiage to include that. Thanks for calling it out!

0 votes
by (14 points)
0 votes

This is the project I wrote, hope he can help you.


by (58 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.