0 votes
extends KinematicBody2D

var max_health = 3
var health = max_health
var knockback = Vector2.ZERO
const SPEED = 100.0

func _physics_process(delta):
    knockback = knockback.move_toward(Vector2.ZERO, 200 * delta)
    knockback = move_and_slide(knockback)
    var direction = ($Player.position - position).normalize()
    var motion = direction * SPEED * delta
    position += motion

func _on_Hurtbox_area_entered(area):
    var knockback_vector = area.knockback_vector
    knockback = area.knockback_vector * 200
    health -= 1
    if health <= 0:
        queue_free()

(Also, if I take out .position from $Player.position it helps that bug but causes another)

in Engine by (16 points)
edited by

Edited: fixed your code formatting.

1 Answer

0 votes

Your problem is that $Player doesn't get a valid node.

$Player is equivalent to get_node("Player") which means "get a child of this node named 'Player'". Player is clearly not a child of this node.

You must use the actual path to the player node. I don't know what that is because you haven't shown what your scene setup is.

Misunderstanding node paths is a common beginner error. This may help:

http://godotrecipes.com/basics/getting_nodes/

by (21,920 points)
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.