It didn't work, and Enemy froze. I mean in the above code, where should I insert the code scale.x = -1 or scale.x = 1 so that when Enemy moves to chase Player when velocity.x flip the scale also flip (animation + collision)? Because if it's a normal move and scale flip, I already know how to do it, but in this case it's a move that chases the Player and knows how to dodge obstacles, so I don't know how to do it. The method I do is already presented above. Navigation2D -> Navigation2DPolygonInstance. Hope you help me.Thank you!
Flip by scale.x and enemies Group chase Player (enemy know to avoid Obstacles!
You can put the get_simple_path() function in the enemy script. So each enemy can get their own shortest path to the player.
yikescloud because this way uses Navigation2DPolygonInstance and it must be in the World node. If you put the simple_path function in an Enemy node, you must use Navigation2D by Tilemap. But the way Tilemap is too complicated for me. Do you have a way in this direction that is simple? Or simply code in Tilemap style. Because the clips on Youtube make the Tilemap style so confusing.
- Edited
Odin You can use it in the enemy. Just like this:
extends KinematicBody2D
onready var nav:Navigation2D = get_node("../../NavigationPolygon")
func _process(delta):
nav.get_simple_path(self.position,$Player.position)
yikescloud Unfortunately it doesn't work. If OK, can you give me an example code to help me in that case?Sincerely thank you very much. Sorry I'm new to coding too. ^^
- Edited
I just do this in my game, I use both navigationPolygonInstance and Tilemap same time, it's below the navigation2D node, and it just works fine.
func navigate_point(var _p:Vector2) -> Vector2:
var _path = _Navigator.get_simple_path(position, _p)
if _path.size() > 1:
return _path[1]
return _p
Yeah, that's great. I really appreciate your great help, Yike! Sorry for my trouble.World's Script? Enemy's Script? What's the full take on this, Yike? I wonder if you'd be willing to show me the full code on this issue for me? All are just code related to this problem only.Once again thank you very much for your help. Best regards!
Odin You dont need a world script for this. Later I will upload a demo project maybe
yikescloud Thank you so much, Master Yike! ^^
- Edited
I put the example project here! Just Check it here
Press WASD to control the player.
It still gives me a lot of errors, Yike? Is something wrong? This is the project you sent me. How will I fix it? and if i want Enemies to not only chase every Player, but they also chase everyone in the Player group, how do I add the which code? Using Groups? Sorry to bother you Yike! ^^
- Edited
Odin
Fix the Error: change script in NavigationPolygonInstance like this:
func draw_self():
for i in navi_poly.get_outline_count():
var _outline:PoolVector2Array = navi_poly.get_outline(i)
if _outline.size() == 0:
continue
_outline.append(_outline[0])
draw_polyline(_outline, Color.white, 5.0, false)
Seems like get_polygon_count () return count of triangles but not the island.
If you want chase other, just change this code in enemy process:
var p = navigate_point(_Player.position)
to
var p = navigate_point(_player_group.get_nodes_in_group[player_index])
yikescloud
That error is gone, now only the error on row 14 in Enemy's Script. Now what Yike?
Odin Just comment this like you select. I thought I commented it already, maybe you uncomment it again?
Odin
See this project to 3d Navigation, maybe it will help you:
https://godotforums.org/d/30668-friend-follows-player-using-pathfinding-3d-navigation-mesh