Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | cgill95 |
I have a tilemap that is generated in a Level Scene in which my player can move just fine and collision detection works just fine too. Now I wanted to add an enemy that will chase the player inside the Level and tried all kinds of different A.I.s for it and it never works. The Enemy never follows the player until it is ther. At first it usually runs away, once the distance gets big enough the enemy changes direction but does not come close to the player. Here is my current code for the enemy.
extends KinematicBody2D
var speed = 100
var player
onready var attackRay = $AttackCollider
func _ready():
player = get_node("/root/Player")
var velocity = Vector2()
func _physics_process(delta):
velocity = position.direction_to(player.position) * speed
move_and_slide(velocity)
In case it matters here is my Player Code
extends KinematicBody2D
signal exitLevel
export var speed = 200
func _physics_process(delta):
var velocity = Vector2()
if Input.is_action_pressed("ui_right"):
velocity.x += 1
if Input.is_action_pressed("ui_left"):
velocity.x -= 1
if Input.is_action_pressed("ui_down"):
velocity.y += 1
if Input.is_action_pressed("ui_up"):
velocity.y -= 1
if velocity.length() > 0:
velocity = velocity.normalized() * speed
move_and_slide(velocity)
Any tips or help in figuring out why this basic AI is not working is greatly appreciated. I use Godot 3.2 btw.