Discrepancy between visual and positions

Godot Version

v4.2.1.stable.mono.official [b09f793f5]

Question

The tween isn’t working as intended; instead of moving each object 100 units to the left, it sends them all to a single point. In the top script, the positions are correct, and visually they’re in the correct positions. However, in the bottom script, the global positions of the enemies are all the same, even though visually they appear at different locations.

[This script is attached to the map]

var xOffset = 100
func SpawnerE():
	if spawner_nodes.get_child(4).get_child(0).get_child_count() < 3:
		for i in range(3):
		var newObj = BTYPEENEMY.instantiate()
		spawner_nodes.get_child(4).get_child(0).add_child(newObj)
		var newPosition = Vector2(i * xOffset, 0)
		newObj.global_position = spawner_nodes.get_child(4).get_child(0).global_position + newPosition
		
		print_rich("[color=green]Global position from map: [/color]", newObj.global_position)

[This script is attached to the enemy]

func MoveToLeft():
	if moving:
		return

	var currentPosition = global_position
	moving = true
	var tween = create_tween()
	print("Current position from enemy: ", currentPosition)
	var targetPosition = currentPosition + Vector2(-100, 0)
	print("Target position from enemy: ", targetPosition)
	tween.tween_property(self, "global_position", targetPosition, 1)

positions

When is MoveToLeft() called? If it’s called from _ready(), you need to set the newObj’s position before adding it as a child.

Also, the code could be cleaner with a couple of small changes. I suggest replacing the line

newObj.global_position = spawner_nodes.get_child(4).get_child(0).global_position + newPosition

with

newObj.position = newPosition

and the Tween could change position instead of global_position.

func MoveToLeft():
	if moving:
		return

	moving = true
	var tween = create_tween()
	var targetPosition = position + Vector2(-100, 0)
	tween.tween_property(self, "position", targetPosition, 1)
1 Like

Moving the MoveToLeft() from ready to process solved it. Thanks.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.