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

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 属于AI

public class Player : MonoBehaviour
{
    private NavMeshAgent agent;
    public Transform target;
    void Start()
    {
        agent = GetComponent<NavMeshAgent>();
        agent.SetDestination(target.position);
        
    }

    // Update is called once per frame
    void 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);
    }
}


http://www.kler.cn/a/589044.html

相关文章:

  • 网络安全设备系统集成方案 系统集成和网络安全
  • 命令行参数和环境变量【Linux操作系统】
  • 【论文笔记】Best Practices and Lessons Learned on Synthetic Data for Language Models
  • BigEvent项目后端学习笔记(一)用户管理模块 | 注册登录与用户信息全流程解析(含优化)
  • 数据增强常见问题与解决方案:提升AI模型性能的关键技巧
  • PyCharm如何有效地添加源与库?
  • CSS中固定定位
  • 错误记录: git 无法连接到github
  • Apache Pol (excel)
  • 算法——图论——关键活动
  • Python的那些事第四十五篇:继承自Nose的测试框架Nose2
  • 区间预测 | Matlab实现QRBiTCN分位数回归双向时间卷积神经网络注意力机制时序区间预测
  • Ubuntu 一站式初始化笔记
  • 【sql靶场】第13、14、17关-post提交报错注入保姆级教程
  • JVM常用概念之超态虚拟调用
  • 解析GNGGA数据,C语言单片机
  • AI是如何实现屏幕触控防水? 实测华为畅享70X
  • Redis监控:从睁眼瞎到千里眼的进化史
  • 【go语言圣经1.6】
  • 19.如何使用 pandas 处理大型 Excel 文件:并行读取工作表