Error by changing position via Code

Question

I wanna change the position of my particle, manually if the player move left or right.
I get the flowing error "Invalid get index “position” (on base: Transform2D)

What’s the correct way to change the position from objects via Code?

#Flip the Sprite & sprint Particle Position
	if direction > 0:
		animated_sprite.flip_h = false
		sprint_particles.transform.position.x = -6
		
	elif direction < 0:
		animated_sprite.flip_h = true
		sprint_particles.transform.position.x = 5
1 Like

Try This:

# Assuming sprint_particles is using a Transform2D
var transform = sprint_particles.transform
if direction > 0:
    animated_sprite.flip_h = false
    transform.origin.x = -6
elif direction < 0:
    animated_sprite.flip_h = true
    transform.origin.x = 5
sprint_particles.transform = transform

Transform2D doesn’t have position but origin (see here). If you want to use position, just remove the transform., because node2d.position is short for node2d.transform.origin.

3 Likes

In Unity, the transform property is king of all :slight_smile: In Godot you rarely have to use it directly. Just modify the position property right on the node!