How to make a player clone that follows the player's movement and kill the player on touch?

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

I’m learning how to code and use Godot.

I’m trying to make a 2D platformer game where the player is trying to finish each level by reaching a portal

and there is a clone (enemy) enters the level after the player starts moving by a couple of seconds and it starts chasing it by doing the same exact movement and kills the player on touch.

:bust_in_silhouette: Reply From: djmick

You’ll need to make an array, and every frame add the player’s current position to the array. Then pass that array to your enemy, and every frame, loop through the array and set the enemy there. Then delete that. Here’s an example:

Add in your player script:

var past_positions = []

func _physics_process(delta):

Add in your enemy script:

var future_positions = []
var current_position = 0

func _physics_process(delta):
    future_positions = get_parent().get_node("Player").past_positions
    global_position = future_positions[current_position]
    current_position += 1

Note that where I have (get_parent().get_node(“Player”)), replace “Player” with whatever you player node’s name is. Also, you’ll have to make sure that the player and the enemy node are both children of your main game node so that the (get_parent().get_node()) call will work.

As for killing the player on touch, add an area2d to the enemy, and attach the body_entered() call to it, and in that function just do body.queue_free(). There are plenty of tutorials on this. I hope this helps, and if you have any questions I’ll try to answer them!

After making some research I found out that I’ll need to use arrays for that. but I didn’t know how to implement it.
That helped a lot … thanks.
I also want to ask how can I implement it using input instead of position?

abdallhhelles | 2021-08-15 09:06