2D Agents with Teleporting?

Hi guys!

I am using a 2D grid graph and was wondering if it is possible for my AI to detect whether using a teleporter is quicker to reach its destination, rather than walking all the way there? I want to do this for the edges of my map, so that the AI reappear on the other side. Any help on this would be greatly appreciated! :slight_smile:

1 Like

Hi

Yes, this is possible.

Since you have a map with wraparound I’m guessing creating these links using code is easier for you. Take a look at this page for how to add new connections using code: https://arongranberg.com/astar/docs/editinggraphs.html
You can use AstarPath.active.gridGraph.GetNode(x, z) to get the relevant nodes.

If you create a link with a cost of 0 (or at least a very low cost) then you will also need to modify the A* Inspector -> Settings -> Heuristic setting. It needs to be set to None because otherwise the A* algorithm assumes that moving towards the goal is usually better than moving away from it, so it will usually never even consider moving in the opposite direction.
See also https://arongranberg.com/astar/docs/pathfinding.html#Heuristic

That’s awesome news thanks very much for this! I will have a go and let you know how it goes!

Note however that the agents will not automatically teleport to the other side of the map. That you’ll have to do yourself. The agents will just think that “oh, now I apparently need to walk to the other side of the map, let’s turn around”.

Hi Aron,

Maybe I am a bit slow, but could you give a more detailed explanation on how you would make this work? I turned off the heuristic setting but if I position a nodelink next to the start position and a nodelink next to the end position, it should take the nodelink (with 0 cost), however it doesn’t do that. I believe it is because the distance between the nodelinks are a part of the equation, which makes the total distance greater if using the nodelinks.

Do you know how to address that?

Hi @aron_granberg This is the setting that I have made in my game, and even though the “portals” are connected with nodelink2 and the cost is 0 (and heuristic setting is none), it won’t choose to use the “portals”. I am guessing that it takes the portal distance into the measurement which makes that route longer.

Hi

So your cost factor field for the link is 0?

Do you think you could show a screenshot of the pathfinding result when you have A* Inspector -> Show graphs enabled, A* Inspector -> Settings -> Debug -> Show search tree enabled and graph coloring mode set to G.

Here’s another situation where I create walls to increase the manual walking distance, which will make it go through the portal.

That doesn’t look like your heuristic is set to None… It should have searched a lot more nodes to the side in that case.

What does it show if you set graph coloring mode to H? (include the scale in the screenshot for reference)

@aron_granberg I can’t believe I’ve made this stupid mistake. You are right. I am sorry to bother you with this newbie mistake :confused:

Np. I’m glad it’s working now :slight_smile:

@aron_granberg I am definitely having the same issue but I definitely have the heuristic set to none
You can see the green line in the middle, so the nodelink2 seems to be working, but my enemies never choose to use it. Has anything changed in the recent versions?

PS: Also yes cost factor is set to 0
PS2: This is 3D so maybe has a different solution?

For anyone in the future, our settings had to be changed in various places, not just the Heuristic for this to work which wasn’t ideal and also needed to call RichAI.Teleport so that when they teleport to stay there and not to snap back to the grid.