How to make sprites flip to player when player is in opposite direction?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By HarryCourt
:warning: Old Version Published before Godot 3 was released.

So, I have some zombies, which their default sprite is them looking left (Ignore the glitches):

However, when I go to the left of the map, zombies are looking the other way:

Here’s the script to one of the zombies, they almost have the same script btw:

extends KinematicBody2D


const GRAVITY = 60

onready var zombie = self
var speed = 0.5
var velocity = Vector2()
onready var target = get_parent().get_node("Player")

export var ZombieSpeed = 1500

export var Smoothness = 0.2

func _ready():
	set_fixed_process(true)
	#self.add_collision_exception_with(Boundry)

func _fixed_process(delta):
    
    
	var direction = (target.get_global_pos() - zombie.get_global_pos()).normalized()
	velocity.y += GRAVITY
    #move(direction*speed*delta)
    
	var motion = velocity * speed * delta

	motion = move(motion)
    
	velocity.x = lerp(velocity.x, ZombieSpeed * direction.x, Smoothness)
    
	if (is_colliding()):
		var normal = get_collision_normal()
		motion = normal.slide(motion)
		velocity = normal.slide(velocity)
		var collider = get_collider()
		move(motion)
		
		if (collider.is_in_group("Bullet")):
			queue_free()
			print("Zombie3 Killed!")
		if (collider.is_in_group("AutoKill")):
			queue_free()
			print("Zombie3 Killed by Boundry!")
:bust_in_silhouette: Reply From: Zylann

You can flip their sprite when their direction changes, like this:

var sprite = get_node("zombie_sprite")
sprite.set_scale(Vector2(sign(direction.x), 1))

Note 1: while it would work, you don’t need to do this every frame in _process, only when the direction changes.

Note 2: don’t scale the body, as it may have unwanted side-effects (as described here Issue with flipping 2D characters (non uniform scaling) · Issue #12335 · godotengine/godot · GitHub)

That doesn’t seem to work, it just sets the zombies in one position.

HarryCourt | 2017-10-26 04:19

Oh Oo but that code isn’t setting the position of the zombie sprite at all. It really flips the sprite horizontally by setting a scale of -1 if direction is left, and a scale of 1 if direction is right. Isn’t this what you wanted? Or did I miss something? What is your node structure?

Zylann | 2017-10-26 19:37

Like this; Screenshot - f8d25b2b3f30da383e0bfbb577f7de99 - Gyazo

HarryCourt | 2017-10-28 12:50

Ok, so is this doing the flip correctly?

var sprite = get_node("CollisionShape2D/PlayerSprite")
sprite.set_scale(Vector2(sign(direction.x), 1))

Another advice:
Don’t place your sprite under the collision shape. The reason is, collision shapes no longer exist when the game is exported. It also creates a parent/child relationship that you could easily get rid of. So instead, you could have this hierarchy:

- Zombie
    - CollisionShape2D
    - PlayerSprite
        - Hands

Zylann | 2017-10-28 17:02