CharacterBody2D Flip not working

Godot Version

Godot 4.2.2

Question

Hey so I have a character what’s movement is that it jumps from a wall to another wall, and I have a script to it what makes the sprite flip if the player uses right click and its happening in the opposite side of the character where the click before that was. I’m unable to describe it xd. I just want the sprite to flip tho.
So here is the script, can somebody help me out with it? It’s not doing everything.

func _input(event):
if event is InputEventMouseButton and event.button_index == MOUSE_BUTTON_RIGHT and event.is_pressed():
var sprite_global_position = global_position # Assuming the script is attached to the character node

	if event.position.x < sprite_global_position.x and event.position.x < last_click_position.x:
		$AnimatedSprite2D.flip_h = true
	else:
		$AnimatedSprite2D.flip_h = false
	
	last_click_position = event.position

I think your problem is that the event.position Vector is in the viewport transform, so you need to convert the sprite transform to the viewport transform using the line get_viewport_transform() * (get_global_transform() * global_position) # called from CanvasItem. you can find more information in the docs.

Your script should look like this:

var last_click_position:Vector2
@onready var sprite = $"path/to/your/sprite"
func _input(event):
	if event is InputEventMouseButton and event.button_index == MOUSE_BUTTON_RIGHT and event.is_pressed():
		var sprite_screen_position = get_viewport_transform() * (get_global_transform() * global_position)

		if event.position.x < sprite_screen_position.x and event.position.x < last_click_position.x:
			sprite.flip_h = true
		else:
			sprite.flip_h = false
		last_click_position = event.position