Making enemies avoid collisions without the A* algorithm

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Name277

I have moved into the last part of this article here: A Bit Awake - News, 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.

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.

Ertain | 2021-03-06 18:21

:bust_in_silhouette: Reply From: Name277

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:
		CHASE:
			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)
					animationState.travel("walk")
					look.force_raycast_update()
				else:
					state = ATTACK
				for scent in player.scent_trail:
					look.cast_to = (scent.position - self.position)
					look.force_raycast_update()
					velocity = velocity.move_toward(look.cast_to.normalized() * MAX_SPEED, ACCELERATION * delta)
					animationState.travel("walk")

					if !look.is_colliding():
						look.force_raycast_update()
						self.dir = look.cast_to.normalized()
						break
			elif attacktimer == 0:
				state = IDLE