Move to Target

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

First, I new to Godot. So I make a simple top-down game and I have one thing that still confuses me. Let’s say I have a Player Node that have an area2D and I want when my enemies heading to my area and I press button, my Player can dash into the target. But I don’t get it how to code that thing. How can I get the enemy position and how can I dash to enemy position?

:bust_in_silhouette: Reply From: exuin

In order to get the enemy position, you need to get the node path for to the enemy. This explains how node paths work. Once you have the node path, you can just get the position property of the enemy (like enemy.position or so).

For dashing, you probably want to set a state on the player so that it can’t do anything else while dashing. So have variable like is_dashing or so. In the _physics_process function have the player move toward the enemy position using Vector2.direction_to().

thanks! it very helpful.

DeerForMera | 2021-10-26 21:49