Skip to content

Instantly share code, notes, and snippets.

@thebeardphantom
Last active April 24, 2017 17:48
Show Gist options
  • Save thebeardphantom/8de07a6b202af5187fa51e7a30d276c6 to your computer and use it in GitHub Desktop.
Save thebeardphantom/8de07a6b202af5187fa51e7a30d276c6 to your computer and use it in GitHub Desktop.
NavMeshAgent's shape visualization is broken! Here's a temporary fix. Attach this to a NavMeshAgent's GameObject to get an accurate visualization.
using UnityEngine;
using UnityEngine.AI;
[RequireComponent(typeof(NavMeshAgent))]
[ExecuteInEditMode]
public class NavMeshAgentVisualizer : MonoBehaviour
{
#if UNITY_EDITOR
private NavMeshAgent agent;
private Mesh cylinderMesh;
private void OnDrawGizmosSelected()
{
if (agent == null)
{
agent = GetComponent<NavMeshAgent>();
}
if (cylinderMesh == null)
{
var obj = GameObject.CreatePrimitive(PrimitiveType.Cylinder).GetComponent<MeshFilter>();
cylinderMesh = obj.sharedMesh;
DestroyImmediate(obj.gameObject);
}
var index = -1;
for (int i = 0; i < NavMesh.GetSettingsCount(); i++)
{
if (NavMesh.GetSettingsByIndex(i).agentTypeID == agent.agentTypeID)
{
index = i;
break;
}
}
if (index != -1)
{
var settings = NavMesh.GetSettingsByIndex(index);
var scale = new Vector3(settings.agentRadius * 2f, settings.agentHeight / 2f, settings.agentRadius * 2f);
var position = transform.position + new Vector3(0f, (settings.agentHeight / 2f) - agent.baseOffset, 0f);
var color = Gizmos.color;
Gizmos.color = Color.cyan;
Gizmos.DrawWireMesh(cylinderMesh, position, Quaternion.identity, scale);
Gizmos.color = color;
}
}
#endif
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment