Unity按固定路线自动寻路
2017-08-25 21:48
274 查看
using UnityEngine; using System.Collections; using System; public class NavigationHyp1 : MonoBehaviour { GameObject[] hypPathPoints; int hypNextPathPointsIndex = 1; // Use this for initialization void Start () { hypPathPoints = GameObject.FindGameObjectsWithTag("HypPath"); //得到的数组是反序的,下面对数组排序,两种方法: //Array.Reveres(hypPathPoints); Array.Sort(hypPathPoints, (x, y) => { return x.gameObject.name.CompareTo(y.gameObject.name); }); //先到达第一个点的位置 transform.position = hypPathPoints[0].transform.position; //方向 transform.forward = hypPathPoints[hypNextPathPointsIndex].transform.position - transform.position; } // Update is called once per frame void Update () { if (Vector3.Distance(hypPathPoints[hypNextPathPointsIndex].transform.position,transform.position)<0.1f) { //判断的是物体是否到达最后一个点 if (hypNextPathPointsIndex!=hypPathPoints.Length-1) { hypNextPathPointsIndex++; } //物体到达最后一个点后停在 最后一个点的位置 if (Vector3.Distance(hypPathPoints[hypPathPoints.Length-1].transform.position,t 4000 ransform.position)<0.1f) { transform.position = hypPathPoints[hypPathPoints.Length - 1].transform.position; return; } //方向的改变 transform.forward = hypPathPoints[hypNextPathPointsIndex].transform.position - transform.position; } transform.Translate(Vector3.forward * 5 * Time.deltaTime, Space.Self); } }
//这是一个物体的的寻路就完成了,然后博主是加了一个克隆和销毁的脚本
using UnityEngine; using System.Collections; public class InsHyp : MonoBehaviour { float timer; public GameObject hypTarget; // Use this for initialization void Start () { } // Update is called once per frame void Update () { timer += Time.deltaTime; if (timer>=2) { timer = 0; GameObject.Instantiate(hypTarget); } Destroy(hypTarget, 10); } }
相关文章推荐
- Unity19--物体自动沿固定路线走,自动寻路
- Unity自动寻路Navmesh之高级主题
- Unity中自动寻路的几种方法(二)
- Unity 自动寻路 Navmesh 之 跳跃,攀爬,斜坡
- Unity 自动寻路Navmesh之跳跃,攀爬,斜坡
- 【unity系统模块开发】自动寻路Navmesh
- Unity 自动寻路Navmesh之跳跃,攀爬,斜坡
- Unity中自动寻路的几种方法(三)
- Unity实战 RTS3D即时战略游戏开发(六) Navigation Mesh 自动寻路
- Unity 3D 自动寻路(简单)
- Unity中使物体自动寻路的方法
- Unity物体跟随鼠标点的位置自动寻路
- Unity 自动寻路Navmesh之高级主题
- Unity Navigation system自动寻路AI
- unity5.4 自动寻路
- Unity中使物体自动寻路的方法
- android百度地图开发之自动定位所在位置与固定位置进行驾车,步行,公交路线搜索
- Unity 自动寻路
- Unity 自动寻路Navmesh之入门
- Unity 3D 中自动寻路 和 跟随转向 探析