Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | NotImportant |
I try to make a game and I have a problem…
I have the scene of an enemy and connected his attackArea with his kinematicbody. In an other scene ( Player ) I have the variable health.
And when the body entered then I try to subtract the variable health by 10
How is this possible???
I tried with get_node("/root/Player)
but it didnt work…
For anybody who needs it, here is my enemy scrpt:
extends KinematicBody2D
export var health = 50
export var speed = 100.0
export var attack_load = 0.4
var velocity = Vector2()
var player = null
var can_attack = true
func _physics_process(delta):
velocity = Vector2()
if player:
velocity = position.direction_to(player.position) * speed
velocity = move_and_slide(velocity)
if health <= 0:
queue_free()
func _on_Area2D_body_entered(body):
player = body
func _on_Area2D_body_exited(body):
player = null
func _on_AttackArea_body_entered(body):
if can_attack == true:
get_node("/root/Player")health -= 5
can_attack = false
yield(get_tree().create_timer(attack_load), "timeout")
can_attack = true