问题描述
我想在英雄走过之前提前知道路线,并通过线Render绘制,告诉我是否可以在不让身体在前面的情况下以任何方式从agent中找出路线他已经划清界限,我将非常感谢任何信息
截图中,英雄使用NavMeshAgent穿过点,我能找到路线或指向目标吗
解决方法
public class DisplayedPath : MonoBehaviour
{
public LineRenderer line; //to hold the line Renderer
public Transform target; //to hold the transform of the target
public NavMeshAgent agent; //to hold the agent of this gameObject
private void Start()
{
line = GetComponent<LineRenderer>(); //get the line renderer
agent = GetComponent<NavMeshAgent>(); //get the agent
// target = transform;
StartCoroutine(getPath());
}
public IEnumerator getPath()
{
// line.SetPosition(0,transform.position); //set the line's origin
agent.SetDestination(target.position); //create the path
yield return new WaitForEndOfFrame(); //wait for the path to generate
DrawPath(agent.path);
agent.Stop();//add this if you don't want to move the agent
}
public void DrawPath(NavMeshPath path)
{
if (path.corners.Length < 2) //if the path has 1 or no corners,there is no need
return;
line.SetVertexCount(path.corners.Length); //set the array of positions to the amount of corners
line.SetPosition(0,transform.GetChild(0).position); //set the line's origin
for (var i = 1; i < path.corners.Length; i++)
{
Vector3 up = path.corners[i];
up.y += 1.5f;
line.SetPosition(i,up); //go through each corner and set that to the line renderer's position
}
}
}
感谢 TEEBQNE。