unity学习之自动寻径

来源:互联网 发布:windows snmp dhcp 编辑:程序博客网 时间:2024/04/19 20:25

       
      今天我们主要学习了自动寻径。
      我们要实现一个功能:点击场景中的一个位置,角色可以自动寻路过去。角色会绕过各种复杂的障碍,找到一条理论上”最短路径“。
       
       主要步骤:
       
       1、创建地形或地板都行。
       2、增加角色。
       3、添加障碍物。
       4、选中地形或地板,打开window窗口的Navigation窗口,设置Navigation Static.
       5、依次选中障碍物,重复上面的操作。
       6、Navigation窗口中,选择Bake(烘焙)界面,点击Bake按钮,进行场景烘焙,就可以烘焙出寻路网格了。
       7、为角色添加NavMeshAgent组件。Component->Navigation->Nav Mesh Agent。
       8、给角色添加脚本。

       脚本代码
 
  1. private NavMeshAgent agent;

  2.         void Start() {
  3.               agent = GetComponent<NavMeshAgent>();

  4.         }

  5.         void Update() {
  6.                 RaycastHit hit;
  7.                 if (Input.GetMouseButtonDown(0)) {
  8.                         Ray ray =            
  9.                                              Camera.main.ScreenPointToRay(Input.mousePosition);
  10.                         if (Physics.Raycast(ray, out hit)){
  11.                                 agent.SetDestination(hit.point);
  12.                                          }
  13.                                 }
复制代码
     
       如果想加动画效果的话,可以通过Animator来实现,代码
  1. using UnityEngine;
  2. using System.Collections;

  3. public class Move : MonoBehaviour {

  4.     private NavMeshAgent a;
  5.     private Animator b;
  6.         void Start () {
  7.         a = GetComponent<NavMeshAgent>();
  8.         b=GetComponent <Animator >();
  9.         }
  10.         
  11.         
  12.         void Update () {
  13.         if (Input.GetMouseButtonDown(0))
  14.         {
  15.             Ray ray = Camera.main.ScreenPointToRay(Input .mousePosition );
  16.             RaycastHit hit;
  17.             if (Physics.Raycast(ray, out hit))
  18.             {
  19.                 a.SetDestination(hit.point);
  20.             }
  21.         }
  22.         if(a.remainingDistance ==0)
  23.         {
  24.             Idle();
  25.         }
  26.         else
  27.         {
  28.             Walk();
  29.         }
  30.         }
  31.     void Idle()
  32.     {
  33.         b.SetFloat("idle", 1f);
  34.         b.SetFloat("walk", 0.0f);
  35.         b.SetFloat("run", 0.0f);
  36.     }
  37.     void Walk()
  38.     {
  39.         b.SetFloat("idle", 0.0f);
  40.         b.SetFloat("walk", 1f);
  41.         b.SetFloat("run", 0.0f);
  42.     }
  43.     void Run()
  44.     {
  45.         b.SetFloat("idle", 0.0f);
  46.         b.SetFloat("walk", 0.0f);
  47.         b.SetFloat("run", 1f);
  48.     }
  49. }
复制代码

          
        如果想要让宠物跟随主人的话,只需在宠物身上加一个脚本


  1. public class Sphere : MonoBehaviour {

  2.     public GameObject    a;
  3.     private NavMeshAgent agent;
  4.         void Start () {
  5.         agent = GetComponent<NavMeshAgent>();
  6.         }
  7.         
  8.         // Update is called once per frame
  9.         void Update () {
  10.         if (a != null)
  11.         {
  12.             agent.destination = a.transform .position;
  13.         }
  14.         }
  15. }
复制代码

0 0
原创粉丝点击