【Unity】unity3d NavMeshAgent 导航显示路径
2017-10-30 09:55
766 查看
首先看一下效果
using UnityEngine;
using UnityEngine.AI;
// Use physics raycast hit from mouse click to set agent destination
[RequireComponent(typeof(NavMeshAgent))]
public class ClickToMove : MonoBehaviour
{
NavMeshAgent m_Agent;
RaycastHit m_HitInfo = new RaycastHit();
public LineRenderer _lineRenderer;
void Start()
{
m_Agent = GetComponent<NavMeshAgent>();
}
void Update()
{
if (Mathf.Abs(m_Agent.remainingDistance) < 1.5f)
{
_lineRenderer.positionCount = 0;
_lineRenderer.gameObject.SetActive(false);
}
if (_lineRenderer.gameObject.activeInHierarchy)
{
Vector3[] _path = m_Agent.path.corners;//储存路径
var path = _path;
_lineRenderer.SetVertexCount(_path.Length);//设置线段数
for (int i = 0; i < _path.Length; i++)
{
Debug.Log(i + "= " + _path[i]);
_lineRenderer.SetPosition(i, _path[i]);//设置线段顶点坐标
}
}
if (Input.GetMouseButtonDown(0) && !Input.GetKey(KeyCode.LeftShift))
{
var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray.origin, ray.direction, out m_HitInfo))
{
m_Agent.destination = m_HitInfo.point;
//m_Agent.Stop();
_lineRenderer.gameObject.SetActive(true);
}
}
}
}
using UnityEngine;
using UnityEngine.AI;
// Use physics raycast hit from mouse click to set agent destination
[RequireComponent(typeof(NavMeshAgent))]
public class ClickToMove : MonoBehaviour
{
NavMeshAgent m_Agent;
RaycastHit m_HitInfo = new RaycastHit();
public LineRenderer _lineRenderer;
void Start()
{
m_Agent = GetComponent<NavMeshAgent>();
}
void Update()
{
if (Mathf.Abs(m_Agent.remainingDistance) < 1.5f)
{
_lineRenderer.positionCount = 0;
_lineRenderer.gameObject.SetActive(false);
}
if (_lineRenderer.gameObject.activeInHierarchy)
{
Vector3[] _path = m_Agent.path.corners;//储存路径
var path = _path;
_lineRenderer.SetVertexCount(_path.Length);//设置线段数
for (int i = 0; i < _path.Length; i++)
{
Debug.Log(i + "= " + _path[i]);
_lineRenderer.SetPosition(i, _path[i]);//设置线段顶点坐标
}
}
if (Input.GetMouseButtonDown(0) && !Input.GetKey(KeyCode.LeftShift))
{
var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray.origin, ray.direction, out m_HitInfo))
{
m_Agent.destination = m_HitInfo.point;
//m_Agent.Stop();
_lineRenderer.gameObject.SetActive(true);
}
}
}
}
相关文章推荐
- Unity3d NavMeshAgent 路径点寻路
- [Unity3D]A*算法、导航网格、路径点寻路对比(A-Star VS NavMesh VS WayPoint)
- [Unity3D]A*算法、导航网格、路径点寻路对比(A-Star VS NavMesh VS WayPoint)
- 一周乱弹(3-18 bootstrap模态框表单提交、jsp页面获取项目路径、bootstrap三级导航菜单、css是否显示属性)
- 【Unity3d开发记录】Unity动态画曲线、弧线(GL画线)在UI上的显示
- Unity中BUG:SetDestination can only be called on an active agent that has been placed on a navmesh 的处理
- [Unity-14] Unity3d中使用摄像机制作实时显示小地图
- [Unity&网格移动]NavMeshAgent寻路 怎么样使得角色移动的角速度不影响移动
- Unity3D-NavMesh导航网格寻路
- A*算法、导航网格、路径点寻路对比(A-Star VS NavMesh VS WayPoint)
- Google搜索结果显示重大改变 使用页面上的导航路径替换页面URL串
- Unity教程之-Unity3d自带寻路Navmesh
- Unity Navmeshagent与飞行
- 【Unity3D】NavMeshAgent常用方法
- unity 鼠标指示NavMeshAgent移动
- Unity关于 "Move" can only be called on an active agent that has been placed on a NavMesh.问题的解决办法
- Unity 3D - 导航系统 ( NavMesh )
- Unity3D-基本导航(NavMesh)功能实现
- NavMeshAgent 寻路导航组件
- [Unity&缺少引用]NavMeshAgent缺少引用