Unity开源的NavMeshComponents可以丰富拓展UnityNav组件的功能
角色统一挂载 navmeshagent脚本 还有设置物体目标点脚本:playermovement
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class PalyerMovement : MonoBehaviour {
private NavMeshAgent agent;
void Start()
{
//获取角色上的NavMeshAgent组件
agent = GetComponent<NavMeshAgent>();
}
void Update()
{
//鼠标左键
if (Input.GetMouseButtonDown(0))
{
//射线检测
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
bool isCollider = Physics.Raycast(ray, out hit);
if (isCollider)
{
//hit.point射线触碰的Position
//SetDestination设置下一步的位置
agent.SetDestination(hit.point);
}
}
}
}
1、动态烘焙=>当场景变化时,实时烘焙,例如 随机地图
地面总物体挂载navmeshsurface 脚本
书写动态烘焙脚本:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
using UnityEngine.UI;
//右键在场景中新建一个Cube,重新烘焙导航网格
public class BuildNavMesh : MonoBehaviour {
public GameObject buildPrefab;
private NavMeshSurface surface;
void Start ()
{
surface = GetComponent<NavMeshSurface>();
}
void Update () {
if (Input.GetMouseButtonDown(1))
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
bool isCollider = Physics.Raycast(ray, out hit);
if (isCollider)
{
GameObject go = Instantiate(buildPrefab, hit.point, Quaternion.identity);
go.transform.SetParent(this.transform);
surface.BuildNavMesh();
}
}
}
}
2、动态障碍物添加组件navmeshObstacle,勾选carve会被自动识别然后重新烘焙,这个应该作用不大,可以设置生成物体的层 后通过动态烘焙也行
如果不是随机地图以上作用不大,一般是烘焙好的场景
3、实现上下楼
烘焙前,设置面板上的数据:如
代理半径agent Radius,越小覆盖面积越大,但是容易断
代理高度agent height不能设置太高,容易断
坡度max slope不能设置太低,容易断
步高step height:针对阶梯上下楼,过低容易断
drop height:掉落高度
jump distance:跳跃距离
4、实现跳跃,点对点 offmeshlink组件
5、实现连续面跳跃:
navmeshlink width可以设置跳跃面 双向
6、实现设置区域成本
数字设置越高,
导航成本越高
选择物体的区域
如何设置落下来但是不一定上得去?
好像只能是动态关闭link组件了