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