Skip to content

Instantly share code, notes, and snippets.

@tsubaki
Last active November 29, 2016 14:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tsubaki/1f32ebf2e12b9c251c7fac3fbd7a6e87 to your computer and use it in GitHub Desktop.
Save tsubaki/1f32ebf2e12b9c251c7fac3fbd7a6e87 to your computer and use it in GitHub Desktop.
NavmeshAgentからパスを取得する
using System.Collections;
using UnityEngine;
using UnityEngine.AI;
[RequireComponent(typeof(NavMeshAgent))]
public class Path2 : MonoBehaviour {
[SerializeField] NavMeshAgent agent;
[SerializeField] Transform target;
const int maxPosition = 9; // パスの最大登録数
// 座標リスト。使い回す
Vector3[] positions = new Vector3[maxPosition];
IEnumerator Start ()
{
agent.updatePosition = false;
agent.SetDestination(target.position);
yield return new WaitWhile(() => agent.pathStatus != NavMeshPathStatus.PathComplete);
agent.path.GetCornersNonAlloc(positions);
agent.Stop();
}
private void OnDrawGizmosSelected()
{
for (int i = 0; i < positions.Length - 1; i++)
{
Gizmos.DrawLine(positions[i], positions[i + 1]);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment