As the titles says, when my ai hits the wall on the right he turns left.. good. but when he hits the wall on the left he gets stuck at left.

ive checked the code many times but cant seem to find where its happening.

if a fresh pair of eyes can take a look and give me some insight it would be much appreciated.

	extends KinematicBody2D
	var speed=100
	var hp
	var levelStart = 0
	var is_hit = 0
	var motion
	
	var facing_left
	var facing_right
	
	onready var sprite = get_node("Sprite")
	
	var anim = "Idle"
	
	func _ready():
		set_fixed_process(true)
		set_process_input(true)
		pass
	
	
	func _fixed_process(delta):
		print(facing_left," ", facing_right)
		if (levelStart == 0):
			anim = "Walk"
			facing_left = true
			levelStart = 1
		if (anim == "Walk"):
			if (facing_left == true):
				motion = Vector2(-1,0)
				sprite.set_flip_h(true)
		
			if (facing_right == true):
				motion = Vector2(1,0)
				sprite.set_flip_h(false)
	
			if (facing_left):
				facing_right = false
			if (facing_right):
				facing_left = false
		
		if (facing_left == true and self.is_colliding()):
			facing_right = true
		if (facing_right == true and self.is_colliding()):
			facing_left = true
			
		
		motion = motion*speed*delta
		move(motion)
		sprite.play(anim)
		pass
10 days later

I'm a little late to this, but look at lines 40-45: if the ai is left and colliding, it is set to face right and then immediately checked again for facing right and colliding - before it's had a chance to move - and set to face left again.

At the end of the first frame where ai is facing left and colliding, both facing_right and facing_left will be true. Then, on the next frame, facing_right is set to false in lines 35-40, and now ai is facing left and colliding again, and the process repeats.

I would get rid of the facing_left and facing_right variables, you already have one variable for the direction - motion. Just use that and test it directly, it will be a lot easier to keep track of what's going on in your code!

6 years later