playMaker

Author Topic: Useful for NavMeshAgent debugging  (Read 2728 times)

tchrisbaker

  • Playmaker Newbie
  • *
  • Posts: 8
Useful for NavMeshAgent debugging
« on: July 02, 2016, 04:07:19 AM »
I created a debugging action for tracking a character's NavMeshAgent path. It will draw a Debug line from the character to each point in the path.

If the path is white, it is complete and therefore successful, yellow is partial and red is invalid.

Code: [Select]
using UnityEngine;

namespace HutongGames.PlayMaker.Actions
{
[ActionCategory(ActionCategory.Debug)]

public class PathUtils : FsmStateAction {
[RequiredField]
[Tooltip("The Game Object to work with. NOTE: The Game Object must have a NavMeshAgent component attached.")]
[CheckForComponent(typeof(NavMeshAgent))]
public FsmOwnerDefault gameObject;

private NavMeshAgent agent;

private Color color;

private void _getAgent()
{
GameObject go = Fsm.GetOwnerDefaultTarget(gameObject);
if (go == null)
{
return;
}

agent =  go.GetComponent<NavMeshAgent>();
}

public override void OnEnter() {
_getAgent ();
}

public override void OnUpdate()
{
DrawPath (agent.path);
}

void DrawPath(NavMeshPath path) {
if (path.corners.Length < 2)
return;
switch (path.status) {
case NavMeshPathStatus.PathComplete:
color = Color.white;
break;
case NavMeshPathStatus.PathInvalid:
color = Color.red;
break;
case NavMeshPathStatus.PathPartial:
color = Color.yellow;
break;
}

Vector3 previousCorner = path.corners[0];

int i = 1;
while (i < path.corners.Length) {
Vector3 currentCorner = path.corners[i];
Debug.DrawLine(previousCorner, currentCorner, color);
previousCorner = currentCorner;
i++;
}
}
}
}


mdotstrange

  • Hero Member
  • *****
  • Posts: 555
    • Can't code? Who cares! Make games anyway!
Re: Useful for NavMeshAgent debugging
« Reply #1 on: July 02, 2016, 07:53:06 AM »
Sounds very useful- thanks  :)
Indie game dev and instructor at the Strange School. Learn Playmaker at the Strange School!

RAWilco

  • Playmaker Newbie
  • *
  • Posts: 20
    • StinBeard
Re: Useful for NavMeshAgent debugging
« Reply #2 on: September 12, 2021, 07:22:28 AM »
I know this is really old, but I only just stumbled across this action and it is incredibly useful.
This really ought to be part of the Pathfinding package.