playMaker

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

tchrisbaker

  • Playmaker Newbie
  • *
  • Posts: 8
    • View Profile
Useful for NavMeshAgent debugging
« on: July 02, 2016, 01: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: 549
    • View Profile
    • Can't code? Who cares! Make games anyway!
Re: Useful for NavMeshAgent debugging
« Reply #1 on: July 02, 2016, 04:53:06 AM »
Sounds very useful- thanks  :)
Indie game dev and instructor at the Strange School. Learn Playmaker at the Strange School!