0 votes

So, I have some zombies, which their default sprite is them looking left (Ignore the glitches):
https://gyazo.com/52006a1138ed076b3519d39d864de2aa

However, when I go to the left of the map, zombies are looking the other way:
https://gyazo.com/324329e639791740e274f21b421627b3

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!")
in Engine by (163 points)

1 Answer

0 votes

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 https://github.com/godotengine/godot/issues/12335)

by (28,966 points)

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

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?

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
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.