Search code examples
game-developmentgodotgdscriptgodot4flappy-bird-clone

Objects Teleporting Back to Original Location When Another Is Spawned


So, in trying to learn Godot, I've been making a flappy bird clone. Every time I spawn a new pair of pipes, though, all the pipes teleport back to their original spawn location. But, they still despawn once their coordinates are offscreen, even though they aren't anymore. So there must be some weird desync in the graphics or something?

Here are the scripts:

pipes.gd

extends Node2D

@export var move_speed = 100
var screen_size

func _ready():
    screen_size = get_viewport_rect().size

func _process(delta):
    position.x -= move_speed * delta
    if position.x < -(screen_size.x / 1.8):
        print("Deleted %s" % self.name)
        queue_free()

pipe_spawner.gd

extends Node2D

var new_pipes_scene = preload("res://Objects/pipes.tscn")
var pipe_number = 0
var new_pipes

func _ready():
    spawn_new_pipes()
    
func _on_pipe_spawn_timeout():
    spawn_new_pipes()

func spawn_new_pipes():
    new_pipes = new_pipes_scene.instantiate()
    new_pipes.position = Vector2(220, randi_range(-109, 109))
    new_pipes.name = "pipe #%d" % pipe_number
    $PipesHolder.add_child(new_pipes)
    print("Spawned pipe #%d" % pipe_number)
    pipe_number += 1
    new_pipes = null

bird.gd

extends RigidBody2D

@export var max_down_rotation = 0
@export var jump_vertical_velocity_increase = 1

func _process(_delta):
        if Input.is_action_just_pressed("jump"):
        self.linear_velocity.y = -jump_vertical_velocity_increase

func _on_body_entered(_body):
    print("not implemented")

I've tried messing with the scripts, tinkering with the values, but the same problem. They originally were rotating once they teleported, too, but I fixed that by locking rotation.


Solution

  • So, I found an easy solution, but I still have no idea why the original problem happens or how to fix it. Basically, since the position of the node is moving (rather than the rigid body through velocity or something), you can just freeze the rigid body and it works just fine.