Forum rules - please read before posting.

Simple solution for Implement A* Pathfinding Project

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.

  1. Player and NCP need Seeker component (only this one)
  2. Need to create a GameObject in the scene with Pathfinder component (and make the configuration)
  3. Edit Enums.cs and add AStar

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();
}
}

Comments

  • Nice! This'd be a great fit for the Integrations wiki.

  • edited June 2022

    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:

    • pop the smooth and funnel components (they come with A*) onto your Player and NPC objects or the seeker will make a path that's a bit too literal for AC
    • Set the Nav Mesh layer in AC settings to whatever your Height Testing layer is for your A* graph

    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?

  • edited September 2022

    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:

    void OnEnable () { AC.EventManager.OnCharacterSetPath += OnCharacterSetPath; }
    void OnDisable () { AC.EventManager.OnCharacterSetPath -= OnCharacterSetPath; }
    
    private void OnCharacterSetPath (AC.Char character, AC.Paths path)
    {
        if (character.gameObject == gameObject && path.gameObject != gameObject)
        {
            // Character is moving along a pre-determined Path
        }
    }
    
  • 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

  • Try use this:
    seeker = _char.GetComponent<Seeker>();
  • Yeah, that works. Silly me for not figuring it out. Thanks!

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Welcome to the official forum for Adventure Creator.