Unity3D之寻路系统(一)

寻路系统

游戏中角色的自动寻路,有很多中成熟的实现算法,如:

  • DFS(深度优先搜索算法
  • BFS(广度优先搜索算法)
  • Dijkstra
  • A*路点寻路
  • D*
  • NAV导航网格寻路
  • 预设导航点的固定路径
  • 。。。

本文将介绍NAV导航网格寻路预设导航点的固定路径在Unity3D中的简单应用,Nav导航网格寻路会进行详细介绍,以及寻路追踪的改进。

预设导航点的固定路径

预设导航点的固定点路径,顾名思义,预先将场景中的路径放置导航点,让主角或是敌人根据当前路径的导航点进行移动。

 

Unity3D之寻路系统(一)_第1张图片

首先我们需要预先放置好导航点,当我们需要到达目的地时,检索目的地子物体的position,依次进行移动,判断是否到达对应的导航点,若是,则进行下一个导航点的移动。如上图的waitpeople为目的地,而下面的子物体为预设导航点。注意,顺序不能乱。

//获得路径点
    public void getPathPoint(Transform[] pathPoint)
    {
        
        outBusCount++;
        //生成公交车
        if (outBusCount <= busCount)
        {
            //生成公交车实例
            GameObject bus= Instantiate((GameObject)Resources.Load("Prefabs/bus"), busStation.transform.position, busStation.transform.rotation);

            BusController busC = bus.GetComponent();
            //设置路径点
            busC.setPathPoint(pathPoint);

            //设置公交车的速度
            busC.Speed = speed;
        }
        else
        {
            outBusCount = busCount;
        }
        
        
    }

 

BusController.cs

void Update () {
        if (isMove)
        {
            PathCatch();

        }
	}


//设置路径点
    public void setPathPoint(Transform[] pathPoint)
    {
        this.pathPoint = pathPoint;
        pathPointCount = pathPoint.Length;
    }


//寻路
    private void PathCatch()
    {

        end = pathPoint[pathPoinIndex].position;

        Vector2 direction = end - begin;

        

        //如果公交到达末点
        Vector2 busPos = new Vector2(transform.position.x, transform.position.y);

        if (isEqul(busPos, end))
        {

            pathPoinIndex++;
            begin = end;
            //公交到达目的地
            if (pathPoinIndex == pathPointCount)
            {
                isMove = false;
                //1s后销毁公交车
                Destroy(gameObject,1f);
                //已派出的公交车数-1
                CustomManager._instance.OutBusCount--;
                
            }
        }

        //移动
        transform.Translate(direction * speed * Time.deltaTime);

    }

//判断浮点向量是否相等
    private bool isEqul(Vector2 a, Vector2 b)
    {

        if (Mathf.Abs(b.x - a.x) < 0.1 && Mathf.Abs(b.y - a.y) < 0.1)
            return true;

        return false;
    }

 

 

NavMesh(导航网格)

NavMesh是3D游戏世界中用于动态物体自动寻路,是人工智能的一种,可以快速地开发出具有AI行为的NPC或是敌人,实现绕过障碍物,爬上与跳下障碍物。

 

简单应用

  • 设置场景

场景中所有静态物体(障碍物,建筑。。。)记得将static勾上

Unity3D之寻路系统(一)_第2张图片Unity3D之寻路系统(一)_第3张图片

Unity3D之寻路系统(一)_第4张图片

  • 打开Navigation控制面板,进行寻路路径的生成

Unity3D之寻路系统(一)_第5张图片选择Navigation

Unity3D之寻路系统(一)_第6张图片选择All Object

Unity3D之寻路系统(一)_第7张图片进行Bake

 

Bake完成后会生成导航网格

Unity3D之寻路系统(一)_第8张图片

  • 添加主角

Unity3D之寻路系统(一)_第9张图片Unity3D之寻路系统(一)_第10张图片

  •  在主角身上添加组件

Unity3D之寻路系统(一)_第11张图片

  • ​​​​​​​添加脚本,使用鼠标左键点击地面让主角移动

PlayerMoveController.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;

public class PlayerMoveController : MonoBehaviour {

    private NavMeshAgent _nav;

	// Use this for initialization
	void Start () {
        _nav = GetComponent();
	}
	
	// Update is called once per frame
	void Update () {
		//获得鼠标点击屏幕的坐标
        if (Input.GetMouseButton(0)) {
            var mouPos = Camera.main.ScreenPointToRay(Input.mousePosition);
            RaycastHit hit;
            if (Physics.Raycast(mouPos, out hit)) {
                _nav.destination = hit.point;
            }
            
        }
        
	}
}

Unity3D之寻路系统(一)_第12张图片

基本的NAV导航网格寻路就大功告成了,下篇是寻路追踪的改进,咱们下篇继续。

你可能感兴趣的:(游戏开发之Unity3d)