I am trying to move a Area2d from right to left. It moves to the right, has a collision en go's to the left. This works.
But it needs to stop at is original position en move to the right again.

i got this so far, but having problems with .position? of global_position? Can anyone point me in the right direction? This is just for learning excersizes.

extends Area2D

var speed = 100
var moveBack = false
var startPos
var colPos

func _ready():
	startPos = get_global_position() #get original starting position
	print(startPos) 


func _process(delta):
	var velocity = Vector2.ZERO
	colPos = $CollisionShape2D.global_position #gets colshape position
	print(colPos) # the current position of the shape
		
	if moveBack == false: 
		velocity.x += 1 # move to the right
	if moveBack == true:
		velocity.x -= 1 # move to the left
		if colPos == startPos:
			print('yehaa') # check of working
			moveBack == false # move to the right
		
	position += (velocity * speed) * delta
	

func _on_Player_body_entered(body):
	""" works """
	print('o no it hit me in the guts') # hits a collisionshape
	moveBack = true
  • DaveTheCoder replied to this.
  • dakkie15 if colPos == startPos:

    That's another problem. Comparing floating point values with the equality operator is not a good idea, since floating point values are approximate. Use "less than" or "greater than" instead.

    Try:

    if colPos.x <= startPos.x:

    Also, you probably should be using _physics_process() instead of _process(), but that's not important if it's only a learning exercise, and wouldn't cause the problem you're seeing.

    dakkie15 moveBack == false # move to the right

    That should be:
    moveback = false # move to the right

    == is a boolean test operator
    = is an assignment operator

    That line of code should have resulted in a warning message (I'm using Godot 3.5):

    Standalone expression (the line has no effect).

      DaveTheCoder thank for the reply. I changed it and it did not work

      I just go's over the original position.

      dakkie15 if colPos == startPos:

      That's another problem. Comparing floating point values with the equality operator is not a good idea, since floating point values are approximate. Use "less than" or "greater than" instead.

      Try:

      if colPos.x <= startPos.x:

      Also, you probably should be using _physics_process() instead of _process(), but that's not important if it's only a learning exercise, and wouldn't cause the problem you're seeing.

        DaveTheCoder Thank you, this worked.

        When i dit get_global_position() it gave me back a int, int. I started working from there.

        global_position is a Vector2 with float components. The values might be printed without decimal places, but internally they're floats.