0 votes

There's an odd interaction between my player and the enemy AI when it comes to chasing. Basically in the scene where the enemy and player is together, it will come close to the player right until it gets to the same distance as when they were first placed in the scene. so for example if my enemy was 5 inches away from the player when i placed it on the scene, it will only chase the player up until the enemy is 5 inches away from the player. if the player tries to get close the enemy will try to move away and get to that 5 inch distance. here is the code i am using for the enemy

    extends KinematicBody2D


var player = null
var move = Vector2.ZERO
var speed = 200

func _physics_process(delta):
    move = Vector2.ZERO

    if player != null:
        move = position.direction_to(player.position) * speed
    else:
        move = Vector2.ZERO

    move = move.normalized()
    move = move_and_collide(move)


func _on_Area2D_body_entered(body):
    if body != self:
        player = body


func _on_Area2D_body_exited(body):
    player = null

if it matters, here is the player code

    extends KinematicBody2D

var movespeed = 400
var friction = 0.05
var acceleration = 0.1
var velocity = Vector2()
var righthand = true
onready var animationplayer = $AnimationPlayer


func get_input():
    var input = Vector2()
    if Input.is_action_pressed("UP"):
        input.y -= 1
    if Input.is_action_pressed("DOWN"):
        input.y += 1
    if Input.is_action_pressed("LEFT"):
        input.x -= 1
    if Input.is_action_pressed("RIGHT"):
        input.x += 1
    return input

func _physics_process(delta):
    var direction = get_input()

    if direction.length() > 0:
        velocity = lerp(velocity, direction.normalized() * movespeed, acceleration)
    else:
        velocity = lerp(velocity, Vector2.ZERO, friction)

    velocity = move_and_slide(velocity)

    look_at(get_global_mouse_position())


func _process(delta):
    if Input.is_action_just_pressed("ATTACK"):

        if righthand == true:
            animationplayer.play("Attack left hand")
            yield(animationplayer, "animation_finished")
            animationplayer.play("Idle hands")
            righthand = false


        elif righthand == false:
            animationplayer.play("Attack right hand")
            yield(animationplayer, "animation_finished")
            animationplayer.play("Idle hands")
            righthand = true
Godot version Godot v3.3.4
in Engine by (12 points)

1 Answer

0 votes

use global_position

by (7,925 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.