0 votes

I want to make a clone follow my player's exact movements using the player's input.

in Engine by (57 points)

1 Answer

+1 vote

Hi...i leave you a very basic example using yield:

extends Node2D
var sprite
var shadow 

func _ready():
    sprite = $Sprite
    shadow =$Sprite.duplicate()
    add_child(shadow)

func _physics_process(delta):
    if Input.is_action_pressed("ui_right"):
        $Sprite.position.x += 1
        yield(get_tree().create_timer(1), "timeout")
        shadow.position.x += 1

    elif Input.is_action_pressed("ui_left"):
        $Sprite.position.x -= 1
        yield(get_tree().create_timer(1), "timeout")
        shadow.position.x -= 1  
by (2,048 points)

What if I want the Shadow to move using the input as well?
and the delay doesn't seem to work.

It is a very basic example and it works, I already tried it. It is a Node2D with a child Sprite. The shadow moves with the same input, only the yield function delays it 1 second. In this case, duplicate the Sprite with duplicate but it could be another sprite or node. Since I don't know what your configuration is, I put this example to give you an idea. If you want more details please post your scene.

It's working perfectly .. thanks you!

but what I meant is how can I make the shadow move with input.
not with shadow.position.x
should I add movement and physics variables in the shadow script? (I have the shadow already created as a child)

Any link to a video, image that shows more or less what you want to achieve?

Like in the game Celeste, when Madeline is being chased by Badeline

Well, I don't know if I saw the correct part of the video but what you want to achieve is nothing trivial. One way would be by registering the inputs and the positions where you execute them in an array. Then the shadow reads that array and executes the movements with the desired time delay. For that the characters have to be, how to say, generalized, abstracted. The characters move because they receive a command to move. Then later in a general input function, you generate the commands, register them and send them to the characters, in this case they would also have to receive the position where the command was executed and/or by time.
There is a pattern called Command:
https://gameprogrammingpatterns.com/command.html

and pattern State:
https://docs.godotengine.org/es/stable/tutorials/misc/state_design_pattern.html

This is all I can tell you based on what I saw in the video (The character chased by various shadows). Ask me if you want details about something specific or you can ask again in the forum to see if there is someone who can give you another answer.

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.
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 webmaster@godotengine.org with your username.