Enemy 2D platforming patrolling platform problems

Godot Version

4.2.1

Question

Hello, so i’m following a tutorial for 2D platforming and everything is working perfectly but for some reason. I can’t get the patrolling enemy to work correctly.

Here is the code:

extends CharacterBody2D

var speed = -60.0

var gravity = ProjectSettings.get_setting(“physics/2d/default_gravity”)

var facing_right = false

func _ready():
$AnimationPlayer.play(“walk”)

func _physics_process(delta):
if not is_on_floor():
velocity.y += gravity * delta

if !$RayCast2D.is_colliding() && is_on_floor():
	flip()
	print("Please Work")
velocity.x = speed
move_and_slide()

func flip():
facing_right = !facing_right

scale.x = abs(scale.x) * -1
if facing_right:
	speed = abs(speed)
else:
	speed = abs(speed) * -1

So when I change, (’ scale.x = abs(scale.x) * -1’ )to (‘scale.x = abs(scale.x) * +1)’

it works but it walks backwards and falls off the other side of the platform.

try this to make it flip
scale.x = scale.x * -1

Okay, I did that but now when they hit the edge, it teleports to the beginning of the tilemap

that’s strange, because what it does basically inverting the scale x value. shouldnt be making it flew thru the map

Figured it out. The node mark for the enemy was far away from the sprite for some reason. So I just had to put the node on top of him. Thanks for you help then.

nice, now that make sense it flip so much