当前位置: 首页 > news >正文

unity基础——Navigation导航系统

1、生成Navigation Mesh:

Window>>AI>>Navigation

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

实现效果:

2、Nav Mesh Agent (导航代理组件)

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、Click To Move 功能简单开发

实现鼠标点击 位置 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、Nav Mesh Obstacle (网格障碍)障碍物

7、Off Mesh Link(网格外链接)跳跃点设置 

创建点位 

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

8、Runtime Nav Mesh Build(运行时构建导航网格)

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();}}}
}

9、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);}
}


http://www.mrgr.cn/news/94885.html

相关文章:

  • windows ai本地化 部署常用Ollama软件详解
  • 4.JVM-垃圾回收介绍
  • Git——分布式版本控制工具使用教程
  • 流量分析实践
  • 函数(函数的概念、库函数、自定义函数、形参和实参、return语句、数组做函数参数、嵌套调用和链式访问、函数的声明和定义、static和extern)
  • AirtestIDE用法
  • 大数据学习(69)-数据架构
  • 蓝桥杯嵌入式赛道复习笔记2(按键控制LED灯,双击按键,单击按键,长按按键)
  • LeetCode Hot 100:1.两数之和、49.字母异位词分组、128.最长连续序列、283.移动零、11.盛水最多的容器
  • 11.anaconda中的jupyter使用、及整合dataspell
  • 【华为OD-E卷 -122 字符统计及重排 100分(python、java、c++、js、c)】
  • 微服务》》Kubernetes (K8S)安装
  • 【蓝桥杯每日一题】3.16
  • 使用Dependency Walker和Beyond Compare快速排查dll动态库损坏或被篡改的问题
  • hubilder打包ios app, 并上传TestFlight
  • 用uv管理python环境/项目(各种应用场景)
  • WebLogic XMLDecoder反序列化漏洞(CVE-2017-10271)深度解析与实战复现
  • PosterRender 实现微信下程序 分享商品生成海报
  • [蓝桥杯 2023 省 B] 飞机降落
  • 算法刷题记录——LeetCode篇(8) [第701~800题](持续更新)