0 votes

I want the object to return after it's out of the camera, but it's not returning. The code is right, because i tried it without the if function and it worked. Another example: I inserted an object outside the camera and it should move into the picture, but it stayed away. Why? Thanks in advance.

asked Jul 30, 2019 in Engine by MonkeyTim (12 points)

Please post your code, it is impossible to help like that.

extends KinematicBody2D

var groundPos = Vector2()
onready var bird = get_node("/root/Bird")

func _physics_process(delta):
    if bird.lastPos / 72 == 1:
        hide()
        groundPos.x = bird.lastPos
        show()
    move_and_slide(groundPos)
    pass

The variable bird.lastPos is the position of the player and in the if loop i just wanted to reset the ground one time because i couldn't handle it yet. The player is continuously moving and the ground should follow everytime it get's out of the screen. My goal was to make Flappy Bird in one scene with this tutorial as a template: https://www.youtube.com/watch?v=HuQgfy2IV9I&list=PLv3l-oZCXaql20IlPe7gfBEzomnPSLekY&index=4&t=2036s
In minute 20:36 he explains it. The only difference is, that i use a KinematicBody2D for the ground.

1 Answer

0 votes

I think your problem is that the player node (Bird) is a child of the ground. The position of the player is relative to the ground so when you move the ground you move the player also.

answered Jul 30, 2019 by BraindeadBZH (1,020 points)

Unfortunately not. They are both in one Node2D. When i remove the if loop it follows the player. But the floor should only come back after it's out of the camera.

Ha ok sorry, I misunderstood.

If you put a print("Test") inside the if statement, do you get an output. It might just be that your condition bird.lastPos / 72 == 1 is never met. By the way what are you testing here? Why 72? Why == 1?

72 because the bird needs 72 steps to leave the ground out of the screen and the width is 144. And == 1 to check when the bird has traveled 72 steps. Because when the bird has traveled 72 steps, the console says true. And it says true after 72 steps.

So why not bird.lastPos >= 72? It would be much safer.

It doesn't work either.

func _physics_process(delta):
    if bird.lastPos >= 72:
        hide()
        groundPos.x = bird.lastPos
        show()
    move_and_slide(groundPos)
    print(bird.lastPos >= 72)
    pass

The console says true after 72 steps.

The code is right, because i tried it without the if function and it worked

If I get that correctly, sounds to me like your condition is never met.
Are you sure bird.lastPos is incrementing? Print the value.

That's the output of print(bird.lastPos) in the first seconds:
0.25
0.25
0.5
0.5
0.75
0.75
1
1
1.25
1.25
1.5
1.5
1.75
1.75
2
2
2.25
2.25
2.5
2.5
2.75
2.75
3
3
3.25
3.25
3.5
3.5
3.75
3.75
4
4
4.25
4.25
4.5
4.5
4.75
4.75
5
5
5.25
5.25
5.5
5.5
5.75
5.75
6
6
6.25
6.25
6.5
6.5
6.75
6.75
7
7
7.25
7.25
7.5
7.5
7.75
7.75
8
8

bird.lastPos is the Vector2().x position of the bird. I can't use the position directly, because an error shows up then.

So, does it go to 72?

Output from the middle of the console:
70
70
70.25
70.25
70.5
70.5
70.75
70.75
71
71
71.25
71.25
71.5
71.5
71.75
71.75
72
72
72.25
72.25
72.5
72.5
72.75
72.75
73
73

Yes, it does

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.