0 votes

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.

Godot version v3.3.2.stable
in Engine by (57 points)
edited by

1 Answer

+1 vote
Best answer

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):
    past_positions.push_back(global_position)

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 (getparent().getnode("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 (getparent().getnode()) call will work.

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

by (596 points)
selected by

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?

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 Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.