0 votes
 extends KinematicBody2D

var player = null

var seePlayer = false
var gravity = 400
var speed = 50
var runSpeed = 70
var velocity = Vector2(0,0)
var isInvincible = false

export var direction = -1
export var detectsCliffs = true
export var health = 30
export var damage = 40

func _ready():
    if direction == 1:
        $AnimatedSprite.flip_h = true
        $PlayerChecker.scale.x *= -1

    $FloorCheck.position.x = $CollisionShape2D.shape.get_extents().x * direction
    $FloorCheck.enabled = detectsCliffs

func _physics_process(delta):
    velocity.y += gravity * delta
    velocity.x = speed * direction
    velocity = move_and_slide(velocity, Vector2.UP)

    if is_on_wall() || not $FloorCheck.is_colliding() && detectsCliffs && is_on_floor() && seePlayer == false:
        direction = direction * -1
        $PlayerChecker.scale.x *= -1
        $AnimatedSprite.flip_h = not $AnimatedSprite.flip_h
        $FloorCheck.position.x = $CollisionShape2D.shape.get_extents().x * direction

    if seePlayer == false:
        $AnimatedSprite.play("Walk")


    if seePlayer == true:
        $AnimatedSprite.play("Run")
        var player = get_node("../../Player2")
        var is_player_on_right = player.position.x > position.x
        var velocity = global_position.direction_to(player.global_position)
        $AnimatedSprite.flip_h = is_player_on_right

        if is_player_on_right:
            direction = 1
        move_and_slide(runSpeed * velocity)



func _on_player_hit(damage):
    if !isInvincible:
        $Timer.start()
        set_physics_process(false)
        speed = 0
        $AnimatedSprite.play("Hit")
        health -= damage
        print("Enemy health = ", health)
        print("Enemy was hit!")
        isInvincible = true
        if health > 0:
            $SoundHit.play()
        else:
            $SoundDeath.play()




func _on_Timer_timeout():
    speed = 50
    set_physics_process(true)
    isInvincible = false
    if health <= 0:
        queue_free()



func _on_PlayerChecker_body_entered(body):
    if body.name == "Player2":
        seePlayer = true


func _on_PlayerChecker_body_exited(body):
    if body.name == "Player2":
        seePlayer = false
in Engine by (14 points)

I'm not sure just an idea
Does it go backwards after hitting the enemy player?

if is_on_wall() || not $FloorCheck.is_colliding() && detectsCliffs && is_on_floor() && seePlayer == false:
        direction = direction * -1
        $PlayerChecker.scale.x *= -1
        $AnimatedSprite.flip_h = not $AnimatedSprite.flip_h
        $FloorCheck.position.x = $CollisionShape2D.shape.get_extents().x * direction

Maybe problem is here

if is_on_wall()

Yeah, according to the grammar, && has higher precedence than ||, so you probably want to use parens(is_on_wall() || not $FloorCheck.is_colliding()) because right now the implicit grouping is is_on_wall() || (not $FloorCheck.is_colliding() && detectsCliffs && is_on_floor() && seePlayer == false)

Please log in or register to answer this question.

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.