├── .gitignore ├── player.png ├── tilemap.png ├── .gitattributes ├── README.md ├── project.godot ├── EnemyAI.gd ├── player.png.import ├── tilemap.png.import ├── icon.svg.import ├── icon.svg └── NavigationTest.tscn /.gitignore: -------------------------------------------------------------------------------- 1 | # Godot 4+ specific ignores 2 | .godot/ 3 | -------------------------------------------------------------------------------- /player.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/stats/godot4-navigation-2d-test/main/player.png -------------------------------------------------------------------------------- /tilemap.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/stats/godot4-navigation-2d-test/main/tilemap.png -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | # Normalize EOL for all files that Git considers text files. 2 | * text=auto eol=lf 3 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # godot4-navigation-2d-test 2 | Project for testing how navigation 2d works in godot 4 3 | -------------------------------------------------------------------------------- /project.godot: -------------------------------------------------------------------------------- 1 | ; Engine configuration file. 2 | ; It's best edited using the editor UI and not directly, 3 | ; since the parameters that go here are not all obvious. 4 | ; 5 | ; Format: 6 | ; [section] ; section goes between [] 7 | ; param=value ; assign values to parameters 8 | 9 | config_version=5 10 | 11 | [application] 12 | 13 | config/name="NavigationTest" 14 | run/main_scene="res://NavigationTest.tscn" 15 | config/features=PackedStringArray("4.0", "Vulkan Clustered") 16 | config/icon="res://icon.svg" 17 | -------------------------------------------------------------------------------- /EnemyAI.gd: -------------------------------------------------------------------------------- 1 | extends CharacterBody2D 2 | 3 | @onready var player:Sprite2D = $"/root/Main/Player" 4 | @onready var target_sprite:Sprite2D = $"/root/Main/TargetSprite2d" 5 | @onready var agent:NavigationAgent2D = $NavigationAgent2d 6 | 7 | func _ready(): 8 | agent.set_target_location(player.global_position) 9 | 10 | func _physics_process(delta): 11 | var target = agent.get_next_location() 12 | var pos = global_position 13 | print(agent.get_nav_path()) 14 | 15 | target_sprite.global_position = target 16 | 17 | var direction = (target-pos).normalized() 18 | 19 | velocity = direction * 3200 * delta 20 | 21 | move_and_slide() 22 | -------------------------------------------------------------------------------- /player.png.import: -------------------------------------------------------------------------------- 1 | [remap] 2 | 3 | importer="texture" 4 | type="CompressedTexture2D" 5 | uid="uid://br4mgox28x1n" 6 | path="res://.godot/imported/player.png-2dd0af52de4b213777cd8c9df94c0978.ctex" 7 | metadata={ 8 | "vram_texture": false 9 | } 10 | 11 | [deps] 12 | 13 | source_file="res://player.png" 14 | dest_files=["res://.godot/imported/player.png-2dd0af52de4b213777cd8c9df94c0978.ctex"] 15 | 16 | [params] 17 | 18 | compress/mode=0 19 | compress/lossy_quality=0.7 20 | compress/hdr_compression=1 21 | compress/bptc_ldr=0 22 | compress/normal_map=0 23 | compress/channel_pack=0 24 | mipmaps/generate=false 25 | mipmaps/limit=-1 26 | roughness/mode=0 27 | roughness/src_normal="" 28 | process/fix_alpha_border=true 29 | process/premult_alpha=false 30 | process/normal_map_invert_y=false 31 | process/hdr_as_srgb=false 32 | process/hdr_clamp_exposure=false 33 | process/size_limit=0 34 | detect_3d/compress_to=1 35 | -------------------------------------------------------------------------------- /tilemap.png.import: -------------------------------------------------------------------------------- 1 | [remap] 2 | 3 | importer="texture" 4 | type="CompressedTexture2D" 5 | uid="uid://dqu0o1jmr1k57" 6 | path="res://.godot/imported/tilemap.png-838486f2ee5f5764f6a08fbb86e9df3c.ctex" 7 | metadata={ 8 | "vram_texture": false 9 | } 10 | 11 | [deps] 12 | 13 | source_file="res://tilemap.png" 14 | dest_files=["res://.godot/imported/tilemap.png-838486f2ee5f5764f6a08fbb86e9df3c.ctex"] 15 | 16 | [params] 17 | 18 | compress/mode=0 19 | compress/lossy_quality=0.7 20 | compress/hdr_compression=1 21 | compress/bptc_ldr=0 22 | compress/normal_map=0 23 | compress/channel_pack=0 24 | mipmaps/generate=false 25 | mipmaps/limit=-1 26 | roughness/mode=0 27 | roughness/src_normal="" 28 | process/fix_alpha_border=true 29 | process/premult_alpha=false 30 | process/normal_map_invert_y=false 31 | process/hdr_as_srgb=false 32 | process/hdr_clamp_exposure=false 33 | process/size_limit=0 34 | detect_3d/compress_to=1 35 | -------------------------------------------------------------------------------- /icon.svg.import: -------------------------------------------------------------------------------- 1 | [remap] 2 | 3 | importer="texture" 4 | type="CompressedTexture2D" 5 | uid="uid://ii8pk2vfku45" 6 | path="res://.godot/imported/icon.svg-218a8f2b3041327d8a5756f3a245f83b.ctex" 7 | metadata={ 8 | "vram_texture": false 9 | } 10 | 11 | [deps] 12 | 13 | source_file="res://icon.svg" 14 | dest_files=["res://.godot/imported/icon.svg-218a8f2b3041327d8a5756f3a245f83b.ctex"] 15 | 16 | [params] 17 | 18 | compress/mode=0 19 | compress/lossy_quality=0.7 20 | compress/hdr_compression=1 21 | compress/bptc_ldr=0 22 | compress/normal_map=0 23 | compress/channel_pack=0 24 | mipmaps/generate=false 25 | mipmaps/limit=-1 26 | roughness/mode=0 27 | roughness/src_normal="" 28 | process/fix_alpha_border=true 29 | process/premult_alpha=false 30 | process/normal_map_invert_y=false 31 | process/hdr_as_srgb=false 32 | process/hdr_clamp_exposure=false 33 | process/size_limit=0 34 | detect_3d/compress_to=1 35 | svg/scale=1.0 36 | editor/scale_with_editor_scale=false 37 | editor/convert_colors_with_editor_theme=false 38 | -------------------------------------------------------------------------------- /icon.svg: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /NavigationTest.tscn: -------------------------------------------------------------------------------- 1 | [gd_scene load_steps=8 format=3 uid="uid://45wk3w6h4txw"] 2 | 3 | [ext_resource type="Texture2D" uid="uid://br4mgox28x1n" path="res://player.png" id="1_1whfv"] 4 | [ext_resource type="Texture2D" uid="uid://dqu0o1jmr1k57" path="res://tilemap.png" id="2_ndgwk"] 5 | [ext_resource type="Script" path="res://EnemyAI.gd" id="3_4jcbk"] 6 | 7 | [sub_resource type="NavigationPolygon" id="NavigationPolygon_0xwgx"] 8 | vertices = PackedVector2Array(-16, -16, 16, -16, 16, 16, -16, 16) 9 | polygons = [PackedInt32Array(0, 1, 2, 3)] 10 | outlines = [PackedVector2Array(-16, -16, 16, -16, 16, 16, -16, 16)] 11 | 12 | [sub_resource type="TileSetAtlasSource" id="TileSetAtlasSource_tmf5a"] 13 | texture = ExtResource("2_ndgwk") 14 | texture_region_size = Vector2i(32, 32) 15 | 0:0/0 = 0 16 | 0:0/0/physics_layer_0/linear_velocity = Vector2(0, 0) 17 | 0:0/0/physics_layer_0/angular_velocity = 0.0 18 | 0:0/0/navigation_layer_0/polygon = SubResource("NavigationPolygon_0xwgx") 19 | 1:0/0 = 0 20 | 1:0/0/physics_layer_0/linear_velocity = Vector2(0, 0) 21 | 1:0/0/physics_layer_0/angular_velocity = 0.0 22 | 1:0/0/physics_layer_0/polygon_0/points = PackedVector2Array(-16, -16, 16, -16, 16, 16, -16, 16) 23 | 24 | [sub_resource type="TileSet" id="TileSet_hadf2"] 25 | tile_size = Vector2i(32, 32) 26 | physics_layer_0/collision_layer = 1 27 | navigation_layer_0/layers = 1 28 | sources/0 = SubResource("TileSetAtlasSource_tmf5a") 29 | 30 | [sub_resource type="CircleShape2D" id="CircleShape2D_1ag6n"] 31 | radius = 15.0 32 | 33 | [node name="Main" type="Node2D"] 34 | 35 | [node name="TileMap" type="TileMap" parent="."] 36 | tile_set = SubResource("TileSet_hadf2") 37 | format = 2 38 | layer_0/tile_data = PackedInt32Array(0, 65536, 0, 65536, 65536, 0, 131072, 65536, 0, 196608, 65536, 0, 262144, 65536, 0, 327680, 65536, 0, 393216, 65536, 0, 458752, 65536, 0, 524288, 65536, 0, 589824, 65536, 0, 655360, 65536, 0, 720896, 65536, 0, 786432, 65536, 0, 851968, 65536, 0, 917504, 65536, 0, 1, 65536, 0, 65537, 0, 0, 131073, 0, 0, 196609, 0, 0, 262145, 0, 0, 327681, 0, 0, 393217, 0, 0, 458753, 0, 0, 524289, 0, 0, 589825, 0, 0, 655361, 0, 0, 720897, 0, 0, 786433, 0, 0, 851969, 0, 0, 917505, 65536, 0, 2, 65536, 0, 65538, 0, 0, 131074, 0, 0, 196610, 0, 0, 262146, 0, 0, 327682, 0, 0, 393218, 0, 0, 458754, 0, 0, 524290, 0, 0, 589826, 0, 0, 655362, 0, 0, 720898, 0, 0, 786434, 0, 0, 851970, 0, 0, 917506, 65536, 0, 3, 65536, 0, 65539, 65536, 0, 131075, 65536, 0, 196611, 65536, 0, 262147, 65536, 0, 327683, 65536, 0, 393219, 65536, 0, 458755, 65536, 0, 524291, 65536, 0, 589827, 65536, 0, 655363, 65536, 0, 720899, 65536, 0, 786435, 0, 0, 851971, 0, 0, 917507, 65536, 0, 4, 65536, 0, 65540, 0, 0, 131076, 0, 0, 196612, 0, 0, 262148, 0, 0, 327684, 0, 0, 393220, 0, 0, 458756, 0, 0, 524292, 0, 0, 589828, 0, 0, 655364, 0, 0, 720900, 0, 0, 786436, 0, 0, 851972, 0, 0, 917508, 65536, 0, 5, 65536, 0, 65541, 0, 0, 131077, 0, 0, 196613, 0, 0, 262149, 0, 0, 327685, 0, 0, 393221, 0, 0, 458757, 0, 0, 524293, 0, 0, 589829, 0, 0, 655365, 0, 0, 720901, 0, 0, 786437, 0, 0, 851973, 0, 0, 917509, 65536, 0, 6, 65536, 0, 65542, 0, 0, 131078, 65536, 0, 196614, 65536, 0, 262150, 65536, 0, 327686, 65536, 0, 393222, 65536, 0, 458758, 65536, 0, 524294, 65536, 0, 589830, 65536, 0, 655366, 65536, 0, 720902, 65536, 0, 786438, 65536, 0, 851974, 65536, 0, 917510, 65536, 0, 7, 65536, 0, 65543, 0, 0, 131079, 0, 0, 196615, 0, 0, 262151, 0, 0, 327687, 0, 0, 393223, 0, 0, 458759, 0, 0, 524295, 0, 0, 589831, 0, 0, 655367, 0, 0, 720903, 0, 0, 786439, 0, 0, 851975, 0, 0, 917511, 65536, 0, 8, 65536, 0, 65544, 0, 0, 131080, 0, 0, 196616, 0, 0, 262152, 0, 0, 327688, 0, 0, 393224, 0, 0, 458760, 0, 0, 524296, 0, 0, 589832, 0, 0, 655368, 0, 0, 720904, 0, 0, 786440, 0, 0, 851976, 0, 0, 917512, 65536, 0, 9, 65536, 0, 65545, 65536, 0, 131081, 65536, 0, 196617, 65536, 0, 262153, 65536, 0, 327689, 65536, 0, 393225, 0, 0, 458761, 0, 0, 524297, 65536, 0, 589833, 65536, 0, 655369, 65536, 0, 720905, 65536, 0, 786441, 65536, 0, 851977, 65536, 0, 917513, 65536, 0, 10, 65536, 0, 65546, 0, 0, 131082, 0, 0, 196618, 0, 0, 262154, 0, 0, 327690, 0, 0, 393226, 0, 0, 458762, 0, 0, 524298, 0, 0, 589834, 0, 0, 655370, 0, 0, 720906, 0, 0, 786442, 0, 0, 851978, 0, 0, 917514, 65536, 0, 11, 65536, 0, 65547, 0, 0, 131083, 0, 0, 196619, 0, 0, 262155, 0, 0, 327691, 0, 0, 393227, 0, 0, 458763, 0, 0, 524299, 0, 0, 589835, 0, 0, 655371, 0, 0, 720907, 0, 0, 786443, 0, 0, 851979, 0, 0, 917515, 65536, 0, 12, 65536, 0, 65548, 0, 0, 131084, 0, 0, 196620, 65536, 0, 262156, 65536, 0, 327692, 65536, 0, 393228, 65536, 0, 458764, 65536, 0, 524300, 65536, 0, 589836, 65536, 0, 655372, 65536, 0, 720908, 65536, 0, 786444, 0, 0, 851980, 0, 0, 917516, 65536, 0, 13, 65536, 0, 65549, 0, 0, 131085, 0, 0, 196621, 65536, 0, 262157, 0, 0, 327693, 0, 0, 393229, 0, 0, 458765, 0, 0, 524301, 0, 0, 589837, 0, 0, 655373, 0, 0, 720909, 65536, 0, 786445, 0, 0, 851981, 0, 0, 917517, 65536, 0, 14, 65536, 0, 65550, 0, 0, 131086, 0, 0, 196622, 65536, 0, 262158, 0, 0, 327694, 0, 0, 393230, 0, 0, 458766, 0, 0, 524302, 0, 0, 589838, 0, 0, 655374, 0, 0, 720910, 65536, 0, 786446, 0, 0, 851982, 0, 0, 917518, 65536, 0, 15, 65536, 0, 65551, 0, 0, 131087, 0, 0, 196623, 65536, 0, 262159, 0, 0, 327695, 0, 0, 393231, 0, 0, 458767, 0, 0, 524303, 0, 0, 589839, 0, 0, 655375, 0, 0, 720911, 65536, 0, 786447, 0, 0, 851983, 0, 0, 917519, 65536, 0, 16, 65536, 0, 65552, 0, 0, 131088, 0, 0, 196624, 65536, 0, 262160, 0, 0, 327696, 0, 0, 393232, 0, 0, 458768, 0, 0, 524304, 0, 0, 589840, 0, 0, 655376, 0, 0, 720912, 65536, 0, 786448, 0, 0, 851984, 0, 0, 917520, 65536, 0, 17, 65536, 0, 65553, 0, 0, 131089, 0, 0, 196625, 65536, 0, 262161, 0, 0, 327697, 0, 0, 393233, 0, 0, 458769, 0, 0, 524305, 0, 0, 589841, 0, 0, 655377, 0, 0, 720913, 65536, 0, 786449, 0, 0, 851985, 0, 0, 917521, 65536, 0, 18, 65536, 0, 65554, 0, 0, 131090, 0, 0, 196626, 65536, 0, 262162, 65536, 0, 327698, 65536, 0, 393234, 0, 0, 458770, 0, 0, 524306, 65536, 0, 589842, 65536, 0, 655378, 65536, 0, 720914, 65536, 0, 786450, 0, 0, 851986, 0, 0, 917522, 65536, 0, 19, 65536, 0, 65555, 0, 0, 131091, 0, 0, 196627, 0, 0, 262163, 0, 0, 327699, 0, 0, 393235, 0, 0, 458771, 0, 0, 524307, 0, 0, 589843, 0, 0, 655379, 0, 0, 720915, 0, 0, 786451, 0, 0, 851987, 0, 0, 917523, 65536, 0, 20, 65536, 0, 65556, 0, 0, 131092, 0, 0, 196628, 0, 0, 262164, 0, 0, 327700, 0, 0, 393236, 0, 0, 458772, 0, 0, 524308, 0, 0, 589844, 0, 0, 655380, 0, 0, 720916, 0, 0, 786452, 0, 0, 851988, 0, 0, 917524, 65536, 0, 21, 65536, 0, 65557, 65536, 0, 131093, 65536, 0, 196629, 65536, 0, 262165, 65536, 0, 327701, 65536, 0, 393237, 65536, 0, 458773, 65536, 0, 524309, 65536, 0, 589845, 65536, 0, 655381, 65536, 0, 720917, 65536, 0, 786453, 65536, 0, 851989, 65536, 0, 917525, 65536, 0) 39 | 40 | [node name="Player" type="Sprite2D" parent="."] 41 | modulate = Color(1, 0, 0, 1) 42 | position = Vector2(499, 227) 43 | texture = ExtResource("1_1whfv") 44 | 45 | [node name="EnemyCharacterBody2D" type="CharacterBody2D" parent="."] 46 | position = Vector2(66, 54) 47 | script = ExtResource("3_4jcbk") 48 | 49 | [node name="NavigationAgent2d" type="NavigationAgent2D" parent="EnemyCharacterBody2D"] 50 | 51 | [node name="CollisionShape2d" type="CollisionShape2D" parent="EnemyCharacterBody2D"] 52 | shape = SubResource("CircleShape2D_1ag6n") 53 | 54 | [node name="EnemySprite" type="Sprite2D" parent="EnemyCharacterBody2D"] 55 | modulate = Color(0.2, 0.223529, 1, 1) 56 | texture = ExtResource("1_1whfv") 57 | 58 | [node name="TargetSprite2d" type="Sprite2D" parent="."] 59 | modulate = Color(0, 1, 0.376471, 1) 60 | position = Vector2(68, 104) 61 | scale = Vector2(0.25, 0.25) 62 | texture = ExtResource("1_1whfv") 63 | --------------------------------------------------------------------------------