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

### UnityNavigation 的使用教程 #### 一、导航网格 (NavMesh) 基础概念 导航网格是一种用于表示可行走区域的数据结构,在Unity中,这种数据结构是从场景中的静态几何体自动生成或烘焙而来[^1]。 #### 二、设置角色控制器 为了使对象能够利用导航功能移动,需为其添加`NavMesh Agent`组件。通过调整Agent属性面板下的参数来定制化不同类型的NPC行为模式,比如最大速度、加速度以及半径等物理特性。 #### 三、创建并配置导航网格 要让游戏内的物体成为路径规划的一部分,则需要标记它们作为“Navigation Static”。这一步骤可通过选中目标GameObject后勾选Inspector视图底部的相应选项完成。之后进入Window -> AI -> Navigation打开编辑器窗口,切换到Object标签页下进一步指定哪些部分应该参与计算最终形成的NavMesh表面[^3]。 对于复杂环境来说,可能还需要手动微调障碍物高度限制或是跳台检测距离等高级设定以优化寻路效果;另外值得注意的是,当修改了任何影响现有NavMesh布局的因素时(例如新增/移除墙壁),记得重新点击 Bake 按钮刷新整个地图的信息缓存。 ```csharp // 获取当前场景内所有的 NavMeshAgents 并打印其位置 using UnityEngine; using UnityEngine.AI; public class PrintAllAgentsPosition : MonoBehaviour { void Start() { foreach(Transform child in transform){ NavMeshAgent agent = child.GetComponent<NavMeshAgent>(); if(agent != null){ Debug.Log(child.name + " is at position: "+agent.transform.position); } } } } ``` 一旦完成了上述准备工作,就可以借助API接口轻松实现基本的任务指派逻辑——只需提供目的地坐标给定的对象即可自动沿最优路线前往那里[^2]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值