+1 vote

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?

asked Jan 16, 2019 in Engine by behelit (69 points)
edited Jan 16, 2019 by behelit

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?

Actually, this is for enemy AI. Say I have the player location (vector2) and the enemy location (vector2). I use getsimplepath 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 moveandslide to get the enemy from point A to point B?

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?

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

Please log in or register to answer this question.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.