Adventure Creator Wikia
Advertisement

A* Pathfinding Project is an excellent tool that provides A* pathfinding.  This page provides an integration that allows for AC characters to make use of it when navigating a 3D scene.

(For 2D games, a standalone A* package can be found on AC's Downloads page).

To use it:

  1. Set up your scene to use AC, and create an A* NavMesh following the A* Pathfinding Project's documenation.
  2. Create a new C# file named NavigationEngine_AstarPP, and copy/paste the first script below.
  3. In the Scene Manager, set the Pathfinding method to Custom, and the Script name to NavigationEngine_AstarPP.
  4. Create a new C# file named AstarPPIntegration, and copy/paste the second script below.
  5. Attach the Astar PP Integration component to your character, as well as the AIPath (2D/3D) component.
  6. Run the scene.  When the character is commanded to move, their Motion control field will switch to Manual, and A* Pathfinding Project will take over.

NavigationEngine_AstarPP.cs:

using UnityEngine;
using AC;

public class NavigationEngine_AstarPP : NavigationEngine
{

	public override Vector3[] GetPointsArray (Vector3 startPosition, Vector3 targetPosition, Char _char = null)
	{
		return new Vector3[2] { startPosition, targetPosition };
	}

}


AstarPPIntegration.cs:

using UnityEngine;
using AC;
using Pathfinding;

public class AstarPPIntegration : MonoBehaviour
{

	[SerializeField] private IAstarAI ai;
	[SerializeField] private AC.Char character;

	private void OnValidate ()
	{
		ai = GetComponent<IAstarAI> ();
		character = GetComponent<Char> ();
	}

	private void OnEnable ()
	{
		EventManager.OnCharacterSetPath += OnCharacterSetPath;
		EventManager.OnCharacterEndPath += OnCharacterEndPath;
		ai.isStopped = true;
	}

	private void OnDisable ()
	{
		EventManager.OnCharacterSetPath -= OnCharacterSetPath;
		EventManager.OnCharacterEndPath -= OnCharacterEndPath;
	}

	private void Update ()
	{
		bool isPathfinding = ai.hasPath && !ai.isStopped;
		character.motionControl = isPathfinding ? MotionControl.Manual : MotionControl.Automatic;

		if (!ai.pathPending && ai.reachedDestination && character.IsPathfinding ())
		{
			character.EndPath ();
		}
	}

	private void OnCharacterSetPath (AC.Char _character, Paths path)
	{
		if (character == _character)
		{
			ai.isStopped = false;
			ai.destination = path.Destination;
		}
	}

	private void OnCharacterEndPath (AC.Char _character, Paths path)
	{
		if (character == _character)
		{
			ai.isStopped = true;
		}
	}

}
Advertisement