Disappearing Sprite

Godot Version

v4.6.1.stable.official [14d19694e]

Disappearing Sprite

Hello

I am new to Godot.

I tried to make a Sprite that moves on the screen to random places. Sometimes it works, but often the Sprite disappears. What is my mistake here?

My code:

func move_to_random_position():
	
	var screen_size = get_viewport().get_visible_rect().size
		
	var new_x = randf_range(0, screen_size.x)
	var new_y = randf_range(0, screen_size.y)
	
	position = Vector2(new_x, new_y)

Most likely that your sprite’s position is at (0,0) for the texture, so that if you max out either of the values it’s moved off the screen. Try subtracting the width of the texture from new_x and the height from new_y.

1 Like

Thank you! It seems to work now.

1 Like