Navigation2d use with move_and_slide, please help!

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By behelit

Rewritten and Edited for clarity. Assume that I have a 2d platformer like the following example:

https://github.com/godotengine/godot-demo-projects/blob/master/2d/kinematic_character/player.gd

Now… Say I have the player location (vector2) and the enemy location (vector2). The enemy movement works just like the player in the above example. I use get_simple_path to build an array of pre-existing points that lead from the enemy location to the player location. How do I use that array with move_and_slide to get the enemy from point A to point B?

Hi. To clarify i little, can i assume that you are on one point you get from get_simple_path and you need to move to another point? Could you provide an example of what you want to achieve?

p7f | 2019-01-16 13:12

Actually, this is for enemy AI. Say I have the player location (vector2) and the enemy location (vector2). I use get_simple_path to build an array of pre-existing points that lead from the enemy location to the player location. How do I use that array with move_and_slide to get the enemy from point A to point B?

behelit | 2019-01-16 16:27

Well, for using move_and_slide you have to pass it a velocity with magnitud and direction, and the floor normal (If is top down game you can ommit floor normal). So lets say you have point A as a Vector2 and point B also as a Vector2, and the speed that it should move, stored as speed, then you can get rotation like this:

rotation = A.get_angle_to_point(B)

Then you can get the motion like this:

motion = Vector2(speed,0).rotated(rotation)

And finally you move:

motion = move_and_slide(motion)

And you repeate that for every point. Something like this were you looking for?

p7f | 2019-01-16 16:37

Well… It’s a platformer style game with jumping involved. I probably should have specified that. I rewrote the original question for clarity.

behelit | 2019-01-16 17:16