欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 教育 > 高考 > unity基础——Navigation导航系统

unity基础——Navigation导航系统

2025/11/9 4:44:14 来源:https://blog.csdn.net/LFJINNAN/article/details/146241547  浏览:    关键词:unity基础——Navigation导航系统

1、生成Navigation Mesh:

Window>>AI>>Navigation

将想要生成网格的物体 改为

 

实现效果:

2、Nav Mesh Agent (控制角色进行导航运动和运动相关设置)

Palyer对象添加Nav Mesh Agent 组件

移动到 Target  代码

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;   //Navigation 属于AIpublic class Player : MonoBehaviour
{private NavMeshAgent agent;public Transform target;void Start(){agent = GetComponent<NavMeshAgent>();agent.SetDestination(target.position);}// Update is called once per framevoid Update(){}
}
  • NavMeshAgent.SetDestination :获取代理在世界坐标系单位中的目标或尝试设置代理在其中的目标。 

3、 Navigation Area的设置和作用

设置完成 要烘培地图 

10:  所花费的时间

4、ClickToMove功能简单开发

实现鼠标点击 位置 Player 移动的效果

实现代码:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;public class Player : MonoBehaviour
{private NavMeshAgent agent;//public Transform target;void Start(){agent = GetComponent<NavMeshAgent>();//agent.SetDestination(target.position);}void Update(){if(Input.GetMouseButtonDown(0)){Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);RaycastHit hit;if(Physics.Raycast(ray, out hit)){agent.SetDestination(hit.point);}}}
}

5、跳跃导航设置

设置完成 要烘培地图 

6、NavMeshObstacle障碍物

7、OffMeshLink跳跃点设置 

创建点位 

添加Off Mesh Link 组件(添加在Parent上)

7、RuntimeNavMeshBuild(运行时构建导航网格)

1、导入

在unity文档中下载 组件

Unity-Technologies/NavMeshComponents: High Level API Components for Runtime NavMesh Building

导入unity项目中  新建scene 

2. 相关设置

Collect Objects:

通常使用 Children

Include Layers:

Use Geometry:

3、代码控制

*Agent Type 要相同

代码实现:

using System.Collections;
using System.Collections.Generic;
using System.Drawing;
using UnityEngine;
using UnityEngine.AI;public class PlaceBuilder : MonoBehaviour
{public GameObject builderPrefab;private NavMeshSurface surface;  // 导航网格表面组件void Start(){// 获取挂载在同一个游戏对象上的NavMeshSurface组件surface = GetComponent<NavMeshSurface>();}void Update(){// 当按下鼠标右键时if (Input.GetMouseButtonDown(1)){// 从摄像机发射射线到鼠标位置Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);RaycastHit hit;// 如果射线碰撞到物体if (Physics.Raycast(ray,out hit)){// 实例化预设体GameObject go =GameObject.Instantiate(builderPrefab, hit.point, Quaternion.identity);//设置父物体go.transform.parent= this.transform;//强制重置缩放(解决父物体缩放继承问题)go.transform.localScale = Vector3.one;//重建导航网格(使新物体参与导航计算)surface.BuildNavMesh();}}}
}

8、Nav Mesh Agent  代码控制角色的移动和旋转

组件/方法作用
Quaternion.LookRotation()根据方向向量生成对应旋转
Quaternion.Lerp()在两个四元数之间进行线性插值
agent.desiredVelocity导航系统计算的理想移动方向向量
rotateSmoothing控制转向速度的参数
Time.deltaTime保证帧率无关的平滑过渡

实现代码:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;public class Player : MonoBehaviour
{// 用于路径计算的导航组件private NavMeshAgent agent;// 旋转插值速度public float rotateSmoothing = 7;// 实际移动速度public float speed = 4;void Start(){agent = GetComponent<NavMeshAgent>();// 禁用自动更新位置/旋转(改为手动控制)agent.updatePosition = false;agent.updateRotation = false;}void Update(){if(Input.GetMouseButtonDown(0)){Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);RaycastHit hit;if(Physics.Raycast(ray, out hit)){// 同步代理位置与玩家实际位置agent.nextPosition = transform.position;// 设置导航目标点agent.SetDestination(hit.point);}}Move();}private void Move(){if (agent.remainingDistance < 0.5) return;// 同步代理位置与玩家实际位置agent.nextPosition = transform.position;// 旋转插值transform.rotation = Quaternion.Lerp(transform.rotation, Quaternion.LookRotation(agent.desiredVelocity), rotateSmoothing * Time.deltaTime);//移动transform.Translate(Vector3.forward * speed * Time.deltaTime);}
}

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com