+1 vote

Hello!
How can I have an arrow draw from one object to another object, in other words, I click object 1 and it draws the arrow from object 1 to object 2. See the 1st image to know what I mean.
enter image description here

Also., I have these 2 images for what will be the arrow

enter image description here

enter image description here

Please help?

in Engine by (44 points)

1 Answer

+2 votes

I just did something like this in a recent project. I found it easier to use custom drawing rather than images, although you should also be able to do something with Line2D using textures.

Here's the code - it breaks down into two parts, the dashed line and the arrow:

func draw_arrow_dashed(start, end, size, color):
    # draw dashed line
    var dir = (end - start).normalized()
    var dist = (end - start).length()
    var dash = dir * dist / 10  # segment size
    var gap = dir * dist / 20  # gap size
    var n = 0
    var pos = start  # drawing position
    var points = PoolVector2Array()
    while n < dist:
        draw_polyline(PoolVector2Array([pos, pos + dash]), color, size, true)
        pos += dash + gap
        n += dash.length() + gap.length()
    # draw triangular arrow end
    var a = end + dir * size/1.5 
    var b = end + dir.rotated(2*PI/3) * size
    var c = end + dir.rotated(4*PI/3) * size
    draw_polygon(PoolVector2Array([a, b, c]), PoolColorArray([color]))

It looks like this in-game:

enter image description here

by (20,315 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 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 webmaster@godotengine.org with your username.