Hey there,
I'm trying to get the enemy to chase the player whenever your in it's range, however it just's teleports to the player right now.
Here is my enemy code for now:
extends KinematicBody2D
const GRAVITY = 80
const SPEED = 300
const FLOOR = Vector2(0, -1)
var velocity = Vector2()
var direction = 1
onready var player = get_parent().get_node("Player")
onready var dir = Vector2(sign(player.position.x - position.x), 0)
func _ready():
pass
func _physics_process(delta):
velocity.x = SPEED * direction
velocity.y = GRAVITY
velocity = move_and_slide(velocity, FLOOR)
if is_on_wall():
direction = direction * -1
$Sprite/BottomRayCast.position.x *= -1
if $Sprite/BottomRayCast.is_colliding() == false:
direction = direction * -1
$Sprite/BottomRayCast.position.x *= -1
#Enemy Attack range (Player body enter and exit)
func _on_Area2D_body_entered(body):
if body == player:
$Sprite.modulate.r += 10
player.emit_signal("damage_taken")
func _on_Area2D_body_exited(body):
if body == player:
$Sprite.modulate.r -= 10
#Enemy Chase range
func _on_ChaseRange_body_entered(body):
if body == player:
move_and_collide(dir * SPEED)
What am I doing wrong?