Navigation 2D and Get Simple Path

TheJabberwockyTheJabberwocky Posts: 1Member
edited June 10 in Programming

Hello and thanks for looking into my problem! Very new to Godot, coming from Unity, and I'm trying to work out how to do some simple pathfinding. I followed these two tutorials mostly while trying to figure this out: and https://www.davidepesce.com/2019/11/19/godot-tutorial-how-to-use-navigation2d-for-pathfinding/

My problem is that after putting together a scene with two tilemaps (one containing only textures and no nav/collision info), and the other built with navmeshes in the tileset (built from a scene).

This is the script managing input and attempting to draw the pathfinding line that is found with get simple path. It's attached to the root Node2D in the scene:

class_name NavigationInputManager
extends Node2D

onready var nav_2D : Navigation2D = $"Player Ship/Navigation2D"
onready var line_2D : Line2D = $Line2D
onready var player : Sprite = $"Player/Player Sprite"

func _unhandled_input(event):
    if (event is InputEventMouseButton and event.button_index == BUTTON_LEFT and event.is_pressed()):
        print ($Player.position)
        print (event.position)
        var path = nav_2D.get_simple_path(player.position, event.position)
        line_2D.points = path
        $Player/PlayerController.path = path
        print(path)

For now, the playercontroller class does nothing, it's just a variable assignment.

Attached are three screenshots, the tileset to show how I have it set up, some output from the console that shows the character's position, the clicked position, and then prints the path; and a screen from the game that shows how the pathfinding line shows up. Any help would be greatly appreciated!

Comments

  • MegalomaniakMegalomaniak Posts: 2,738Admin

    I've edited the post to include code formatting.

  • juliustingjuliusting Posts: 1Member
    edited July 31

    I have the same problem.

    I included:
    print(event, new_path, player.global_position, event.global_position)
    it returned "[InputEventMouseButton:5419][](106.307999, 87.049202)(129, 147)"

    I checked out how others are doing it and it seems like we're missing something.

    EDIT: I figured it out. The tiles must exactly align. In my tilemap, I used grid snapping + custom cell sizes to make sure it snaps to the exact dimensions. After that, the pathfinding works perfectly. I got isometric view to work too

Leave a Comment

BoldItalicStrikethroughOrdered listUnordered list
Emoji
Image
Align leftAlign centerAlign rightToggle HTML viewToggle full pageToggle lights
Drop image/file