# Making enemies avoid collisions without the A\* algorithm

**URL:** <https://forum.godotengine.org/t/making-enemies-avoid-collisions-without-the-a-algorithm/12905>\
**Category:** Archive\
**Created:** [March 6, 2021, 9:57am UTC](https://forum.godotengine.org/t/making-enemies-avoid-collisions-without-the-a-algorithm/12905 "2021-03-06T09:57:13Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![system](https://forum.godotengine.org/uploads/default/original/2X/4/4b0a7fa8a6334634b94a13d421f0d99e6f04e21f.svg) [@system](https://forum.godotengine.org/u/system)\
**Post date:** [March 6, 2021, 9:57am UTC](https://forum.godotengine.org/t/making-enemies-avoid-collisions-without-the-a-algorithm/12905/1 "2021-03-06T09:57:13Z")

</div>

| | | |
| --- | --- | --- |
| ℹ | **Attention** | Topic was automatically imported from the old Question2Answer platform. |
| 👤 | **Asked&nbsp;By** | Name277 |

I have moved into the last part of this article here: [A Bit Awake - News](https://abitawake.com/news/articles/enemy-ai-chasing-a-player-without-navigation2d-or-a-star-pathfinding), making the enemies detect the scent:  
 ![Scent Detection](https://i.imgur.com/cwn2CRg.png)

the enemy is not being declared so I tried using self instead like this:  
 ![Scent Detection using self instead](https://i.imgur.com/00T2jN5.png)

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](https://i.imgur.com/kaNrNiA.png)

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](https://i.imgur.com/NNorBWf.png)

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

---

<div class="post-metadata">

**Author:** ![system](https://forum.godotengine.org/uploads/default/original/2X/4/4b0a7fa8a6334634b94a13d421f0d99e6f04e21f.svg) [@system](https://forum.godotengine.org/u/system)\
**Post date:** [March 6, 2021, 6:52pm UTC](https://forum.godotengine.org/t/making-enemies-avoid-collisions-without-the-a-algorithm/12905/2 "2021-03-06T18:52:42Z")

</div>

| | | |
| --- | --- | --- |
| 👤 | **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:

```auto
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

```
