Looking in the forum how to adapt the A * Pathfinding Project system (https://arongranberg.com/astar/) I could make this little script and it work perfect.
public enum AC_NavigationMethod { UnityNavigation, meshCollider, PolygonCollider, AStar, Custom };
I use it because my maps are complex 2d tileset and it was a lot of work to make the holes.
I hope this is useful, regards!
The little script
using AC;
using Pathfinding;
using UnityEngine;
public class NavigationEngine_AStar : NavigationEngine
{
Seeker seeker;
public override Vector3[] GetPointsArray(Vector3 startPosition, Vector3 targetPosition, Char _char = null)
{
if (_char != null)
{
seeker = _char.GetComponent();
if (seeker == null)
{
Debug.LogError("Can not path find, seeker script missing");
return null;
}
}
Path path = seeker.StartPath(startPosition, targetPosition);
AstarPath.BlockUntilCalculated(path);
return path.vectorPath.ToArray();
}
}
It looks like you're new here. If you want to get involved, click one of these buttons!
Comments
Nice! This'd be a great fit for the Integrations wiki.
A year later and I'm just going to add something to this that might help people like this script helped me(thanks gferrari). Sorry never added to a wiki before I've no idea where to start.
If you want this to also check the position is on the A* grid, and find the closest position if it's not, adapt the code to this (this really works best with the grid graph or layered grid graph, it might go slightly mad with the recast graph option).
Also be sure to:
Adapted code to ensure position is on A* Graph and find the closest position if it's not
using AC;
using Pathfinding;
using UnityEngine;
public class NavigationEngine_AStar : NavigationEngine
{
Seeker seeker;
public override Vector3[] GetPointsArray(Vector3 startPosition, Vector3 targetPosition, Char _char = null)
{
if (_char != null)
{
seeker = _char.GetComponent();
if (seeker == null)
{
Debug.LogError("Can not path find, seeker script missing");
return null;
}
}
targetPosition = GetDestinationOnGraph(targetPosition);
Path path = seeker.StartPath(startPosition, targetPosition);
AstarPath.BlockUntilCalculated(path);
if (path != null)
{
return path.vectorPath.ToArray();
}
else
{
Debug.LogError("Can not path find");
return null;
}
}
public Vector3 GetDestinationOnGraph(Vector3 somePosition)
{
if (AstarPath.active.GetNearest(somePosition, NNConstraint.Default).node != null)
{
var node = AstarPath.active.GetNearest(somePosition, NNConstraint.Default).node;
return (Vector3)node.position;
}
else
{
return Vector3.zero;
}
}
}
Hey guys. I tried this and it works perfectly for Walk To Point. But for Walk Along Path, my character doesn't move. I realized that it does have its destination set to the path nodes. If I add an AI Destination setter with a target that I move around, the character starts moving and tries to walk along the path. But without that it doesn't move. Am I missing something?
The "Walk Along Path" feature doesn't involve pathfinding - the point array is gotten from the Path's nodes, rather than calculated at runtime.
How are you moving the character? Does the AI Destination setter control movement, with AC's Player component's Motion control set to Manual?
You can hook a script into the OnCharacterSetPath custom event to update your setter as necessary. For example, placed on the character:
I get this error while trying to integrate A* pathfinding using the script above.
Assets\Scripts\NavigationEngine_AStar.cs(13,28): error CS0411: The type arguments for method 'Component.GetComponent()' cannot be inferred from the usage. Try specifying the type arguments explicitly.
It's referring to this line specifically
seeker = _char.GetComponent();
I'm not sure what I did wrong. I'm using Unity version 2020.3.33f1
seeker = _char.GetComponent<Seeker>();
Yeah, that works. Silly me for not figuring it out. Thanks!
Hey! Working with this I realized that adventure creator takes control of the NPCs rotation after the Face Direction/ Face Object action is triggered.
Before triggering a Face Direction/ Face Object action, Astar works fine and character is rotated by the AIPath script from Astar, but if a Face Direction/ Face Object action is used, adventure creator holds the rotation and character cannot be turned manually again. How can I give rotation control back to A* after the LookAt is finished.
Which script(s) are you using? An integration for A* can be found on the AC wiki:
https://adventure-creator.fandom.com/wiki/A*_Pathfinding_Project_integration
The character's ability to rotate through separate script is based on their "Motion control" field. Setting this to Manual allows them to be moved outside of AC. The wiki script should control this automatically as needed - what's this value set to at the time the issue occurs?