André Pimentel
André Pimentel

Reputation: 61

enemy follow player my "player" when it enters the "enemy" detection zone the enemy continues forward (to the left)

what is happening is that my "enemy" is not following the "player", when my player enters the detection area the "enemy" continues straight ahead (to the left).

thank you, if you understand something or need more information let me know

detection zone enemy code

enemy code:


const EnemyDeathEffect = preload("res://Bots/EnemyDeathEffect.tscn")

export var MAX_SPEED = 60
export var ACCELERATION= 25
export var FRICTION = 700

enum {
    IDLE,
    WANDER,
    CHASE
}

var velocity = Vector2.ZERO
var knockback = Vector2.ZERO

var state = CHASE
var path: PoolVector2Array

onready var sprite = $AnimatedSprite
onready var stats = $Stats
onready var playerDetectionZone = $PlayerDetectionZone 

func _physics_process(delta):
    knockback = knockback.move_toward(Vector2.ZERO, FRICTION * delta)
    knockback = move_and_slide(knockback)
    
    match state:
        IDLE:
            velocity = velocity.move_toward(Vector2.ZERO, FRICTION * delta)
            seek_player()
            
        WANDER:
            pass
        
        CHASE:
            var player = playerDetectionZone.player
            if player != null:
                var direction = (player.position - global_position).normalized()
                velocity = velocity.move_toward(direction * MAX_SPEED , ACCELERATION * delta)
                print(direction)
            else:
                state = IDLE
            sprite.flip_h = velocity.x > 0
                
    velocity = move_and_slide(velocity)

func seek_player():
    if playerDetectionZone.can_see_player():
        state = CHASE
    
func _on_Hurtbox_area_entered(area):
    stats.health -= area.damage
    knockback = area.knockback_vector * 100

func _on_Stats_no_health():
    queue_free()
    var enemyDeathEffect = EnemyDeathEffect.instance()
    get_parent().add_child(enemyDeathEffect)
    enemyDeathEffect.global_position = global_position

Upvotes: 2

Views: 1829

Answers (1)

Theraot
Theraot

Reputation: 40170

I only possible culprit I see is this line:

var direction = (player.position - global_position).normalized()

Here player.position is in its parent local coordinates, while global_position as the name says is in global coordinates. You want this instead:

var direction = (player.global_position - global_position).normalized()

I see this is the way you have on the linked image.

Or if you prefer:

var direction = global_position.direction_to(player.global_position)

Aside from that, it could be that it is changing direction too slowly. Which - given the code - is the same as saying that the ACCELERATION is low, but that is for you to tweak.


I guess it is worth debugging that playerDetectionZone is getting the player, and the CHASE is working correctly.

Common approaches include using a breakpoint, or print. You already have a print for direction. Try also printing player to check the enemy is chasing what you expect it to chase.

For this particular case I also suggest to go to the run the project from the editor, and then go to the Scene panel on the Remote tab, and select the enemy. That would allow you to the properties of the enemy on real time on the Inspector. You should see the state change to CHASE (which would have the value 2), and velocity should also steadily change.

Enabling "Visible Collision Shapes" form the debug menu may also help debugging.

If it is not working, double check the playerDetectionZone has monitoring enabled. Also check that the collision_mask of the playerDetectionZone and the collision_layer of the player are correct.

Upvotes: 2

Related Questions