导航系统

1.导航系统的简单实现

实现俩个点之间让物体自动移动

效果图


图1-1

1.先创建如图1-1所示场景


图1-2

2.物体层次分布如图1-2所示         设置left与right为图1-3所示


图1-3

3为player.添加Nav Mesh Agent组件设置对应值(图1-4)

图1-4

4.烘焙场景点击Window-》Navigition到达如图1-5所示箭头指向部分全部打钩


图1-5

5.,在点击到Bake界面出现如图1-6所示根据需求设置

图1-6

实现脚本

using UnityEngine;

using System.Collections;

public class playerMove : MonoBehaviour{ 

   public Transform[] targets;//目标  

  NavMeshAgent _Player_agent; 

   int Index = 0; 

   public void Awake()    {        

_Player_agent = GetComponent();

_Player_agent.speed = 4;

}

// Use this for initialization

void Start()

{

_Player_agent.destination = targets[Index].position;//寻路目标

InvokeRepeating("Patrol", 7f, 7f);

}

//巡逻方法

void Patrol()

{

if (_Player_agent.remainingDistance<0.5f)

{

if (Index < targets.Length - 1)

{

Index++;

}

else

{

Index = 0;

}

_Player_agent.destination = targets[Index].position;//寻路目标

}

}

}

6.把脚本挂在Player上在如图1-7所示地方值改为2


图1-7


7.会出现如图1-8所示

图1-8

将lefttager与righttager分别拖进去,表示的是俩个移动目标的点,运行后,物体会在这俩个点之间移动

NavMeshAgent重要属性



图1-9

二.实例二

效果图(图中4个物体会经过不同的路线去对面的高台上在返回到原来的位置通过移动球体可以阻挡物体的移动)


图2-1

1.设置俩快地板为如图2-2所示

图2-2

2.在有高台的那块地板上添加组件off Mesh Link如图2-3移动行俩个空物体的位置(高台与地面跳转的俩个位置)拖进如图中箭头所指位置


图2-3

3.添加烘焙场景点击Window-》Navigition到达如图2-4所示箭头指向部分添加俩个层bridg1与bridg2


图2-4

4.设置俩个桥的Navigation Area(层)分别为bridg1与bridg2如图2-5


图2-5

5.在Nav Mesh Agent 下设置层如图2-6

图2-6


图2-7

6.为球体添加动态阻碍物,添加组件Nav mesh Obstacle如图2-8(通过移动球体的位置可以阻碍物体的移动)

图2-8

7.为物体添加脚本并将俩个位置拖进去

图2-9

代码实现

using UnityEngine;

using System.Collections;

public class play : MonoBehaviour{ 

   public Transform[] _play;  

  NavMeshAgent _nma;

    int Index=0;  

  public void Awake()  

  {       

_nma = GetComponent();

_nma.speed = 4;

}

// Use this for initialization

void Start()

{

_nma.destination = _play[Index].position;

InvokeRepeating("A",7f,7f);

}

void A()

{

if (_nma.remainingDistance<0.5f)

{

if (Index < _play.Length - 1)

{

Index++;

}

else

{

Index = 0;

}

_nma.destination = _play[Index].position;

}

}

// Update is called once per frame

void Update()

{

_nma.destination = _play[Index].position;

}

}

三.实例三

实例图(点击按钮会改变线路,使得桥进行切换,桥的设置如实例二一样,添加俩个层分别使用不同的层)

图3-1

在之前的基础上,就是添加了画线的功能

在脚本挂好如图3-2所示后1与2为俩个位置,3与4为俩个桥


图3-2

如图3-3为物体添加Line Renderer组件如图箭头所指可以改变画出来线的粗细


图3-3

实现代码1

using UnityEngine;

using System.Collections;

public class CubePath : MonoBehaviour{  

  NavMeshAgent _navMeshAg;   

LineRenderer _lineRender;//画线  

  NavMeshPath _NavMeshPath;    //   

 public void Awake()    {       

 _navMeshAg = GetComponent();      

  _lineRender = GetComponent();

_NavMeshPath = new NavMeshPath();

}

void Update()

{

DrawPath();

}

void DrawPath()

{

bool havaPath = _navMeshAg.CalculatePath(_navMeshAg.destination,_NavMeshPath);

if (havaPath)

{

Vector3[] pathCorners = _NavMeshPath.corners;

_lineRender.SetVertexCount(pathCorners.Length + 2);//路径里的拐点不包含起始点

_lineRender.SetPosition(0, transform.position);//起点

 for (int i = 1; i <pathCorners.Length+1; i++)

            {

                _lineRender.SetPosition(i, pathCorners[i - 1]);

            }

            _lineRender.SetPosition(pathCorners.Length+1,_navMeshAg.destination);

        }

    }

}

实现代码2

using UnityEngine;

using System.Collections;

public class CubeMove : MonoBehaviour{ 

   public Transform[] targets; 

   NavMeshAgent _agent;

    float time = 0; 

   public float timeInterVal = 2; 

   int index = 0;  

 public GameObject brige1;

    public GameObject brige2;

    public void Awake()  

  {       

 _agent = GetComponent();

}

void Start()

{

_agent.areaMask = 9;//Walkble brige1

brige2.transform.Rotate(0, 90, 0);

_agent.destination = targets[index].position;

}

void Update()

{

if (_agent.remainingDistance<0.5)

{

time += Time.deltaTime;

if (time>=timeInterVal)

{

time = 0;

index++;

index %= targets.Length;

_agent.destination = targets[index].position;

}

}

}

bool isBrige1_open = true;

bool isBrige2_open = false;

//按钮开关函数

public void Brige1_Open()

{

if (!isBrige1_open)

{

_agent.areaMask = 9;//Walkble brige1

brige1.transform.localRotation = new Quaternion();

brige2.transform.Rotate(0, 90, 0);

isBrige1_open = true;

isBrige2_open = false;

}

}

public void Brige1_Close()

{

if (isBrige1_open)

{

_agent.areaMask = 17;

brige2.transform.localRotation = new Quaternion();

brige1.transform.Rotate(0, 90, 0);

isBrige1_open = false;

isBrige2_open = true;

}

}

public void Brige2_Open()

{

if (!isBrige2_open)

{

Brige1_Close();

}

}

public void Brige2_Close()

{

if (isBrige2_open)

{

Brige1_Open();

}

}

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,962评论 19 139
  • Nav Mesh Agent 自动寻路(添加在移动的物体上) Off Mesh Link分离路面(添加在跳跃的起始...
    胤醚貔貅阅读 292评论 0 0
  • title: 翻译|React-navigation导航系统(2)date: 2017-03-28 07:48:3...
    smartphp阅读 7,971评论 2 23
  • 秋风桂香浓 夜长霜露深 静水浮弦月 风过竹影舞 闲步入寒园 幽思潜入眉 口念如来语 轻吟佛祖经 吾身本性善 也有龌...
    玫瑰圣典CC阅读 141评论 0 0
  • 阅读 喜欢上看书,那是高中时候的事了。刚进高中,不知怎么的,很不适应新环境,也没什么朋友。适逢高中每个学期都有必读...
    高晓松他侄子阅读 322评论 1 5