Bashing my head into the computer on this.

Have an enemy object and when it hits a wall (RigidBody2d), it should change directions. I have tried RayCast2d and Area2D and can't seem to figure it out.

My movement functions are all working (stun, walk, idle) but I can't seem to wrap my head around the direction change. I am not sure why it's not working. All help appreciated.

func _physics_process(delta):
	var _physics_ = move_and_collide(_velocity)
	
	if _pursuingPlayer == true:
		_move_state(delta)
	else:
		_idle_state()
	
	
	if _physics_:
		var _object = _physics_.collider.name
		if _object == 'Lydia':
			_last_chance()
		if _object == 'WallObject':
			if _facingLeft:
				_walk_right(delta)
			else:
				_walk_left(delta)


func _move_state(delta):
	if _facingLeft == true:
		if _nowStunned == false:
			if _isAsleep == false:
				_walk_left(delta)
			else:
				_idle_left()
		else:
			_stunned_left()


	if _facingLeft == false: ## switch to right
		if _nowStunned == false:
			if _isAsleep == false:
				_walk_right(delta)
			else:
				_idle_right()
		else:
			_stunned_right()
  • duane replied to this.
  • SnapCracklins

    I think I see a problem. You have

    if _facingLeft == true:
    ...
    if _facingLeft == false:

    If you're setting facing left to false anywhere in the first block, it will trigger the second block. You could fix that with

    if _facingLeft == true:
    ...
    elif _facingLeft == false:

    SnapCracklins

    I think I see a problem. You have

    if _facingLeft == true:
    ...
    if _facingLeft == false:

    If you're setting facing left to false anywhere in the first block, it will trigger the second block. You could fix that with

    if _facingLeft == true:
    ...
    elif _facingLeft == false: