Implementing AIPath in 2D (4.0)

Hey Aron,

I just purchased the upgrade to 4.0 with the goal of getting AIPath to work with 2D. I got it to work (sort of), but have a question and a plea:

  1. The question: do we need to rotate the grid to -90,0,0 or 90,0,0 for XY movement? From reading the code, it seems like -90,0,0 is correct, however in the 2D RVO example scene it is set to 90,0,0.

  2. The plea: is there ANY way to implement AIPath in 2D without having to use transform.forward? Unless I’m doing it wrong, the only way an agent will currently move is if it’s flipped -90,0,0 (otherwise transform.forward will be converted to 0,0 by the system and the AI won’t move). But for our project flipping the AI like this causes all sorts of problems with collisions and other code which assumes the AI is only going to be rotating on the z axis.

I paid for the upgrade explicitly to get the new 2D features and this is really a crushing blow.

Okay, I believe I have figured out how to do this. You need to override two methods, inheriting from AIPath.

  1. MovementUpdate(float deltaTime) - in the override, leave everything the same except for the line :

velocity2D = MovementUtilities.ClampVelocity(velocity2D, speed, slowdown, true, movementPlane.ToPlane(tr.forward));

change the tr.forward to tr.up:

velocity2D = MovementUtilities.ClampVelocity(velocity2D, speed, slowdown, true, movementPlane.ToPlane(tr.up));

  1. RotateTowards(Vector2 direction, float maxDegrees) - in the override replace the line:

Quaternion targetRotation = Quaternion.LookRotation(movementPlane.ToWorld(direction, 0), movementPlane.ToWorld(Vector2.zero, 1));

with:

Quaternion targetRotation = Quaternion.LookRotation(tr.forward, movementPlane.ToWorld(direction, 0));

Once I made those two changes, AIPath seems to work fine in XY space without any need to rotate objects. I’ll be honest and say I don’t fully know why those changes work, so please chime in if you know, or if these changes are a bad idea.

Hi

Ah. It seems I have configured the 2D example scene incorrectly. It should be (-90,0,0). Both -90 or +90 will work however the ‘up’ direction of the graph (the plane normal) will be either along the -Z axis or the +Z axis depending on which one you choose. Usually you want the -Z axis.

  1. I thought about that but I figured that one can easily create a rotated child object on which you can place the sprite or collider or anything else which requires a particular axis to be the forward direction. I try hard not to add too many options to my scripts, however in this case maybe this trade-off wasn’t the best choice? The alternative would be to add a new field

    public int forwardAxis = 2; // 0 = x, 1 = y, 2 = z

at the cost of some additional code complexity.

Also. I just updated the documentation with a new documentation page and a video tutorial showing how to configure pathfinding in 2D.
https://www.arongranberg.com/astar/documentation/4_0_7_2297d27/pathfinding-2d.php

Let me know what you think.

Thanks Aron, having the option to determine the forward/rotation vector would definitely be a big help. In my first reply you can see the two overrides I made to get AIPath to work on the XY without rotation objects :-).

I just uploaded v4.0.8 which includes a ‘rotationIn2D’ checkbox for the AIPath script. Try it out and see if that helps! :slight_smile:

Hey Aron,

I just downloaded the latest version from the asset store and didn’t see that option - does it take a few days to load up?

Thanks!

Hi

Asset Store updates take weeks I’m afraid. It has to go through a review process by the asset store staff. You can download the latest version from the package website.

Hey Aron,

Sorry for taking a while to respond, but I was able to download the new version and by checking off the Rotate in 2D option I was able to delete my two overrides and get the same results w/out any issues. I’ll let you know if anything else comes up, but it’s greatly appreciated!

1 Like