How do you flip a sprite according to the location of the mouse

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By amaou310

How would you flip the sprite left and right according to the mouse position.

KinematicBody2D
> PlayerSkin(Sprite)
    > GunSkin(Sprite)
:bust_in_silhouette: Reply From: ramazan
func _process(delta):
                                  # Sprite = PlayerSkin(Sprite)
if get_global_mouse_position().x > $Sprite.global_position.x:
	print("right")

else:
	print("left")
pass

Thank you very much for the answer. It worked!
However, I wanted to play running_backward animation, when player is moving left(backward) and the mouse cursor is at right(forward).

something like this? how do I find that mouse position is opposite to the movement direction of the player ?

var is_running_backward := is_on_floor() and not is_zero_approx(_velocity.x) and "when the mouse position is opposite direction of the movement?" 

my current code is like this.

func _physics_process(delta: float) -> void:
var _horizontal_direction = (
	Input.get_action_strength("move_right")
	- Input.get_action_strength("move_left")
)

_velocity.x = _horizontal_direction * speed
_velocity.y += gravity * delta

var is_falling := _velocity.y > 0.0 and not is_on_floor()
var is_jumping := Input.is_action_just_pressed("jump") and is_on_floor()
var is_double_jumping := Input.is_action_just_pressed("jump") and is_falling
var is_jump_cancelled := Input.is_action_just_released("jump") and _velocity.y < 0.0
var is_idling := is_on_floor() and is_zero_approx(_velocity.x)
var is_running := is_on_floor() and not is_zero_approx(_velocity.x)
var is_running_backward := is_on_floor() and not is_zero_approx(_velocity.x) and 


if is_jumping:
	_jumps_made += 1
	_velocity.y = -jump_strength
elif is_double_jumping:
	_jumps_made += 1
	if _jumps_made <= maximum_jumps:
		_velocity.y = -double_jump_strength
elif is_jump_cancelled:
	_velocity.y = 0.0
elif is_idling or is_running:
	_jumps_made = 0
		
_velocity = move_and_slide(_velocity, UP_DIRECTION)


if get_global_mouse_position().x > $Position2D/PlayerSkin.global_position.x:
	print("right")
	position2D.scale.x=1
else:
	print("left")
	position2D.scale.x=-1

amaou310 | 2022-06-28 10:54

opposite direction * -1 . (“multiplied by -1”)

maybe use

var mouse = true

 func _process(delta):
 if mouse == true: 
	 if get_global_mouse_position().x > $Sprite.global_position.x:
		print("right")
	
	else:
		print("left")

if Input.is_action_pressed("ui_right"):
	print("walk")
	mouse = false
elif Input.is_action_pressed("ui_left"):
	print("back walk")
	mouse = false

else: 
	mouse = true

pass

########## OR

func _process(delta):
if ! is_running or is_running_backward:
	if get_global_mouse_position().x > $Sprite.global_position.x:
		print("right")
	
	else:
		print("left")

pass

ramazan | 2022-06-28 12:19

My apologies. I should’ve made it more clear.
As you can see from the gif, player’s foot moves forward when looking at right and moving to right. But when player is looking at right and moving to left, player’s foot still moving forward.

Id like to play “Player-Run Backward” animation when input “move_left” and $sprite is right

amaou310 | 2022-06-28 13:07

Update!

I added some code like this. and It seems working as “backward” is printed on output, except the animation is not working and i have no idea why.

if get_global_mouse_position().x > $Position2D/PlayerSkin.global_position.x:
		
	print("right")
	position2D.scale.x=1
		
else:
	print("left")
	position2D.scale.x=-1
	

if get_global_mouse_position().x > $Position2D/PlayerSkin.global_position.x:
	if Input.get_action_strength("move_left"):
		print("backward")
		_animation_player.play("Player-Run Backward")

elif get_global_mouse_position().x < $Position2D/PlayerSkin.global_position.x:
	if Input.get_action_strength("move_right"):
		print("backward")
		_animation_player.play("Player-Run Backward")

amaou310 | 2022-06-28 13:21

check your code.
maybe an example like this can help

if position2D.scale.x == 1 :
      if press.forward:
            print("one")
      elif press.back:
            print("two")
elif position2D.scale.x == -1 :
       if press.forward:
            print("one one")
      elif press.back:
           print("two two")
 

ramazan | 2022-06-28 13:50

Thank you. Thank you so much. It finally worked. I really appreciate your support.
Before I read your answer, I was doing something like this stupid. lmao

if get_global_mouse_position().x > $Position2D/PlayerSkin.global_position.x:
	print("right")
	position2D.scale.x=1
	if Input.is_action_just_pressed("move_right"):
		if is_running:
			print("forward")
			_animation_player.play("Player-Run (copy)")

if get_global_mouse_position().x < $Position2D/PlayerSkin.global_position.x:
	print("left")
	position2D.scale.x=-1
	if Input.is_action_just_pressed("move_left"):
		if is_running:
			print("forward")
			_animation_player.play("Player-Run (copy)")

if get_global_mouse_position().x > $Position2D/PlayerSkin.global_position.x:
	print("right")
	if Input.is_action_just_pressed("move_left"):
		if is_running:
			print("backward")
			_animation_player.play("Player-Run Backward")

if get_global_mouse_position().x < $Position2D/PlayerSkin.global_position.x:
	print("left")
	if Input.is_action_just_pressed("move_right"):
		if is_running:
			print("backward")
			_animation_player.play("Player-Run Backward")

Idea that you gave me is much much cleaner. Now this.

if position2D.scale.x==1:
	if Input.is_action_just_pressed("move_right"):
		print("forward1")
		_animation_player.play("Player-Run (copy)")
	elif Input.is_action_just_pressed("move_left"):
		print("backward1")
		_animation_player.play("Player-Run Backward")
		
elif position2D.scale.x==-1:
	if Input.is_action_just_pressed("move_left"):	
		print("forward2")			
		_animation_player.play("Player-Run (copy)")
	elif Input.is_action_just_pressed("move_right"):
		print("backward2")
		_animation_player.play("Player-Run Backward")

amaou310 | 2022-06-28 14:37

You’re welcome. I wish you success in your project.

ramazan | 2022-06-28 14:43