1. 程式人生 > >Unity (三) NavMeshAgent之:分層路面導航(王者榮耀,英雄聯盟中小兵分三路進攻敵方)

Unity (三) NavMeshAgent之:分層路面導航(王者榮耀,英雄聯盟中小兵分三路進攻敵方)

新建 com back phi nim tro walk void cnblogs

效果:

技術分享

運用分層路面導航讓角色走不同的導航路線

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); //結束行走 } } }


Unity (三) NavMeshAgent之:分層路面導航(王者榮耀,英雄聯盟中小兵分三路進攻敵方)