How can I lock the pathfinding to only move my agent on 45º or 90º?

Godot Version

4.1.2

Question

How can I lock the pathfinding to only move my agent on 45º or 90º?

Hello everyone! I need to implement a top down 8 way movement for the enemies of my game. I could successfully implement that with the tilemap navigation layer property and navigation agent on the enemy. And it worked pretty well except for one thing. When the pathfinding aims to the next point on the chain it will point for any point of the square that represents a tile. I need that the enemy walks only on 45 or 95 degrees, it means what pathfinding can only point to the center of the next square. Is it possible using the current Godot 4.1.2 pathfinding?

Untitled

Thanks for helping me.

try change to EdgeCentered?
image

Hey! Thank you. Almost there. But it’s not pointing to the center of the squares.

image

in that case, you will need to create your own Astar, similar to this:

1 Like

Oh God… I’ve already done that on 3.5 :stuck_out_tongue:

I do not want to do that again.
But no problem I’ll try to adapt it, think it will solve my needs.

Thanks a lot!

1 Like

There is another way:

#set direction to the vector to the next point.
direction = (round(direction / (PI / 4))) * (PI / 4)
#PI / 4 is for 45 degrees, PI / 2 for 90 degrees
#This is in radians, so you could have PI / 6 for 30 degree segments
velocity = (direction * SPEED).normalized()
move_and_slide()

This will round the first thing/a (in this case, direction) to the nearest multiple of the second thing/b (PI / 4). The formula is (round(a / b)) * b

1 Like