This site is currently in read-only mode during migration to a new platform.
You cannot post questions, answers or comments, as they would be lost during the migration otherwise.
0 votes

So I have been following HeartBeast's action rpg tutorial, trying to learn the engine, and I've been following along with him. I got to the section where we started making the Enemy AI.

The enemy scene instances as scene called PlayerDetectionZone which has a CollisionShape2d as a child of it. When the player enters the zone defined by the CollisionShape2d the bat should start moving toward the player, but it does not.

Here is some of the code for my enemy

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

var state = IDLE

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.global_position - global_position).normalized()
                velocity.move_toward(direction * MAX_SPEED, ACCELERATION * delta)

    velocity = move_and_slide(velocity)


func seek_player():
    if playerDetectionZone.can_see_player():
        state = CHASE

Here is my code for the player detection zone:

extends Area2D

var player = null

func can_see_player():
    return player != null

func _on_PlayerDetectionZone_body_entered(body):
    player = body

func _on_PlayerDetectionZone_body_exited(body):
    player = null

I've been putting print statements through out the code too try and isolate which parts are failing, but everything seems to be called correctly. Signals are fired when the player enters, the chase state is called. I have no idea what is causing the problem. The enemy simply does not move. If you need any more information I can provide it.

Thanks.

Godot version 3.4.2.stable
in Engine by (15 points)

1 Answer

+1 vote
Best answer

In that CHASE state, I think you want this:

velocity = velocity.move_toward(direction * MAX_SPEED, ACCELERATION * delta)

Notice, that I've added the velocity = part at the beginning...

by (22,704 points)
selected by
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.