Using Navigation 2d to draw Line2d

Godot Version

4.4

Question

` im trying to click to move movement system with my new project. a little video.
https://streamable.com/qkcopn

character can leave a line behind it, but what i wanna achieve is, before the “click” to “move” when player click the move button and a half transparant version of the player sprite shows up,

between that sprite and orjinal player i wanna draw a line, like telling to player “you can move there”.

And i want to use the navigation agent to do so, because only paths can be walk will be in the navigation mesh. so only posibile patsh that player will see is in the navigation mesh. so i thong why not use navigation agent for this too, its a briliant idia !!! :frowning: .over 6 hours, i did try many things, can’t have the any result that solve this.

bdw i dont know if this is a bug, but im geting noting retrun from the navigation_agent.get_current_navigation_path()

i think, if i can get navigation_agent calculate the path that from player to current_mause_pos i can use that points to draw a line, but let alone doing that,

i cant even draw a line from player to current_mause_pos, godot do not let me draw outside of the _draw function,

i cant use the line.draw_line(vector2,vector2,color)

so yeah, my mind right now like a mushy mushroom, after hours of this problem, and i wanna ask here if there is a solution for this?

`

  1. When mouse moves, set navigation agent target position to mouse position.
  2. Get path from navigation agent. Not sure if this only updates on the next frame.
  3. In a loop create Line2D’s that connect each point in the path.
  4. Add Line2Ds as children.

That’s what I’d go with to start. Of course, you say you’ve been at it for 6 hours and tried this and that, but you don’t actually say what you’ve tried nor are you showing any code, so.

1 Like

thank you for your reply, tbt, i dont remember exacly what i did, but i think i did try the consept you write down, and after i did implent your method, i think i gotten close to solve this,
this is the code that draw a line when move button clicked,

func draw_to_mause(line_2d : Line2D):
	nav_agent_for_mause_draw.get_next_path_position()
	path_for_mause_array = nav_agent_for_mause_draw.get_current_navigation_path()

	line_2d.width = 2.0

	for i in range(0, path_for_mause_array.size() -1):
		var next_point = path_for_mause_array[i + 1]
		line_2d.add_point(next_point)

this is the result : https://streamable.com/87krry
as you can see, line do not go out of navigation map, but when its come to the inner parts its try to catch the paths, sometimes do, sometimes dont.

anyway im okay with that performance,
im update this function with timer that loop every 0.05 second, even after i call this function from _physics , its still gives back the same performance.

I think is either this is most godot can do, or problem with navigation map, anyway this is not the problem now, thanks for the tip, it save me from this problem, only leave me alone with another.

when move button clicked, line2d needs to drawn from player_sprite to mause_pos, but it allways start with from mause_pos and end with mause_pos, i need this to more dynamic, while player moves, line needs to move with player, now i need the solve this, any tip will be helpfull, thanks.

It looks like in your preview you’re drawing a line from previous mouse position to current mouse position. If you wanted a path preview, you should draw from player position to mouse position every time you update.

For updating the path as the player moves, I’d try something like…

  1. When player starts moving, take the path from the agent and save it somewhere.
  2. Draw line from player’s position to next path position and so on.
  3. When player reaches the next path position (for example distance_to() is smaller than some delta), update the next path position from the saved path.

This way you’re drawing from player’s position to where the player is going.

thank you so muc for your repy, after a lot of try and error, and a really helpfull tutorial finally i did it what i want, this is the tutorial if anyone interested; https://www.youtube.com/watch?v=1-f4WEy0GBg

and this is the result of the work https://streamable.com/eq2i5g

after polish the few things im gonna share thi script here, thanks :slight_smile:

if anyone interested this is the project.