why aren't my obstacles moving?

Godot Version

Godot 4.2.2stable

Question

hello! recently i’ve been working on a chrome dinosaur game jumping over obstacle type-of game. i have been having trouble working on this script for my obstacle which is supposed to move from one side of the screen to the other.
the obstacles that are moving are all of the obstacle variations stacked on top of eachother, and the script generates a random number to see which obstacle should be shown. it will then show one, and hide the rest. then, it will start a five second timer, and then it will move all of the obstacles offscreen to the right.
it will then move from right to left, go offscreen to the left, and reset its position back offscreen to the right.

however, i am really confused because i don’t see any obstacles going across the screen. i have probably messed up in the code somewhere and it could either be:

  1. the obstacles are hidden
  2. the obstacles aren’t moving at all

i have written all this code by myself because tutorials weren’t working, so it may be a bit sloppy at points. it should also be mentioned that this is my first time coding in godot 4.
if any of you could find the error that would be a great help.

thank you in advance!

extends Node2D

@onready var timer = $Timer
@onready var offscreen_left = -1920
@onready var offscreen_right = 540

# Called when the node enters the scene tree for the first time.
func _ready(): # when the game is ready:
	new_game() # begin the game

func new_game():
	get_node("cone").visible = false   # }
	get_node("barrel").visible = false # }
	get_node("crate").visible = false  # } hide everything (just in case)
	get_node("hole").visible = false   # }
	get_node("spikes").visible = false # }
	position.x = offscreen_right
	if not global.dead == 1:
		timer.start() # starts a five-second timer


func _on_timer_timeout(): # when the timer has finished?
	if not global.dead == 1: # make sure the player hasn't died in the five seconds since timer started
		randomize() # this was a last minute addition to see if it would change anything but it did not.
		var obstacle_type = (randi() % 5) # picks random number between 0-4 (hopefully) to determine which obstacle should be visible
		if obstacle_type == 0: # if the obstacle is cone?
			get_node("cone").visible = true # start showing cone, hide the rest
			get_node("barrel").visible = false
			get_node("crate").visible = false
			get_node("hole").visible = false
			get_node("spikes").visible = false
		elif obstacle_type == 1: # if the obstacle is barrel?
			get_node("cone").visible = false
			get_node("barrel").visible = true # start showing barrel, hide the rest
			get_node("crate").visible = false
			get_node("hole").visible = false
			get_node("spikes").visible = false
		elif obstacle_type == 2: # if the obstacle is crate?
			get_node("cone").visible = false
			get_node("barrel").visible = false
			get_node("crate").visible = true # start showing crate, hide the rest
			get_node("hole").visible = false
			get_node("spikes").visible = false
		elif obstacle_type == 3: # if the obstacle is hole?
			get_node("cone").visible = false
			get_node("barrel").visible = false
			get_node("crate").visible = false
			get_node("hole").visible = true # start showing hole, hide the rest
			get_node("spikes").visible = false
		elif obstacle_type == 4: # if the obstacle is spikes?
			get_node("cone").visible = false
			get_node("barrel").visible = false
			get_node("crate").visible = false
			get_node("hole").visible = false
			get_node("spikes").visible = true # start showing spikes, hide the rest
		spawn_obstacle() # begin the process of moving obstacle from right to left


func spawn_obstacle():
	if not position.x <= offscreen_left: # if it is not offscreen to the left:
		position.x = offscreen_right # set obstacle position to the right just in case
		position.x -= global.total_speed # start moving from left to right
		if position.x <= offscreen_left: # while moving obstacle, check if it is offscreen yet
			position.x = offscreen_right # if it is, set position back to right
			get_node("cone").visible = false   # }
			get_node("barrel").visible = false # }
			get_node("crate").visible = false  # } hide whatever obstacle is visible (just in case)
			get_node("hole").visible = false   # }
			get_node("spikes").visible = false # }
			timer.start() # and repeat the previous process in another five seconds

Remove the not from this line. I’d put a print around there to see when it does and doesn’t run.

Do you want to move the objects smoothly across the screen, or do you want them to only move every 5 seconds? Because right now, the code you’ve written is doing the latter… And if the speed is low, I could imagine that you can wait quite a while before seeing your object.

If you want them to move every frame, then the code that moves them should happen in _process or _physics_process (or in some function that gets called from one of those).

1 Like