0 votes

I have moved into the last part of this article here: https://abitawake.com/news/articles/enemy-ai-chasing-a-player-without-navigation2d-or-a-star-pathfinding, making the enemies detect the scent:
Scent Detection

the enemy is not being declared so I tried using self instead like this:
Scent Detection using self instead

I did not use the _init function since the enemy and params are not declared, the script is attached to a KinematicBody2D node and I got this error:
Scent Detection Issue

By what I can understand is that there is no target being defined. This is what I used before for the enemy chasing instead of the Getting Started script from the article:
Simple Chase State

I tried using some parts of the script that I had before with the one in the article but I couldn't get it working.

Godot version v3.2.3
in Engine by (21 points)
edited by

If you're going to show code, please copy and paste it (and properly mark it up) in the message body instead of using screenshots.

1 Answer

0 votes

I managed to solve this problem by making up this code, I had to declare the variables in this sort of way and I don't seem to be getting any issues:

var dir

onready var player = get_node("../../Player")

func _ready():
    animationTree.active = true

func _physics_process(delta):
    match state:
            var playerdet = playerDetectionZone.playerdet
            if playerdet != null and attacktimer == 1:
                var look = self.get_node("RayCast2D")
                look.cast_to = (player.position - self.position)
                var distance = (player.position - self.position).length()
                animationTree.set("parameters/idle/blend_position", look.cast_to.normalized())
                animationTree.set("parameters/walk/blend_position", look.cast_to.normalized())
                animationTree.set("parameters/attack/blend_position", look.cast_to.normalized())
                animationTree.set("parameters/hit/blend_position", look.cast_to.normalized())

                if distance > 50:
                    velocity = velocity.move_toward(look.cast_to.normalized() * MAX_SPEED, ACCELERATION * delta)
                    state = ATTACK
                for scent in player.scent_trail:
                    look.cast_to = (scent.position - self.position)
                    velocity = velocity.move_toward(look.cast_to.normalized() * MAX_SPEED, ACCELERATION * delta)

                    if !look.is_colliding():
                        self.dir = look.cast_to.normalized()
            elif attacktimer == 0:
                state = IDLE
by (21 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 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 webmaster@godotengine.org with your username.