Unity (三) NavMeshAgent之:分层路面导航(王者荣耀,英雄联盟中小兵分三路进攻敌方)

效果:

 

 

 

运用分层路面导航让角色走不同的导航路线

 

1、新建一个静态地图

 

 

 

 

 

2、设置3个不同的层

 

 

 

 

3、给不同的路面设置不同的导航层

 

 

4、在导航组件里给角色设置Area Mask,设置角色可以走哪些层

 

1)设置char_ethan不能走Sap(下路),middle(中路)层

 

 

 

 

 

2)设置SapphiArtchan不能走Char(上路),middle(中)层

 

 

 

 

 

5、给SapphiArtchan和char_ethan添加脚本:

 

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

using UnityEngine.AI;

public class NavigationTest : MonoBehaviour {

  private Animator animator; //行走动画

  private NavMeshAgent agent; //导航组件

  private Transform target; //目标位置

  void Start () {

  animator = GetComponent<Animator>();

  agent = GetComponent<NavMeshAgent>();

  target = GameObject.Find("target").transform;

  }

void Update () {

  if (Input.GetKeyDown(KeyCode.Space)) //按空格键

  {

  agent.SetDestination(target.position); //开始导航

  animator.SetBool("walk", true); //行走动画开启

  }

  if (Vector3.Distance(target.position,transform.position)<=1.5f) //如果到达目标1.5m

  {

  agent.isStopped = true; //结束

  animator.SetBool("walk", false); //结束行走

 

  }

}

}

 


 

 

 

 

posted @ 2017-08-23 19:25  鱼歌。  阅读(961)  评论(0编辑  收藏  举报