Unity 自动搜索最优路径方式

Posted suiyi78

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Unity 自动搜索最优路径方式相关的知识,希望对你有一定的参考价值。

通过寻找本物体到达目标物体的最优路径考虑的
通过搜索所以离自己最近的路径,通过最近路径到达终点路径过程中最近的路径,反复搜索最近到达终点


using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class NewMove : MonoBehaviour
{
public InputField valueinput;

public GameObject[] game;
public List<GameObject> lisadd = new List<GameObject>();
public int pos = 0;
public bool bol = false;

void Start()
{

}


public float speed = 0.02f;
private int lisvalue = 0;
private bool bolon_off=false;
void Update()
{
GetMovepath();
Move();

}
public void Move()
{
if (bolon_off) {
if (lisadd.Count >= 1)
{
if ((transform.transform.position - game[pos].transform.position).sqrMagnitude > 0.2)
{
if ((transform.position - lisadd[lisvalue].transform.position).sqrMagnitude < 0.2)
{
lisvalue++;
print(lisvalue);
}
else
{
print("移动");
transform.position = Vector3.MoveTowards(transform.position, lisadd[lisvalue].transform.position, speed);
}
}
else
{

}
}

}
if( (transform.position-game[pos].transform.position).sqrMagnitude<=0.2f)
{
print("到终点了");
lisvalue = 0;
bolon_off = false;
}

}

public void GetMovepath()
{
if (bol)
{

lisadd.Clear();
bol = false;
float distance = 0;
int gamevalue = 0;
int gamevalue1 = 0;
foreach (GameObject gamethis in game)//1
{
if (distance == 0)
{
distance = (transform.position - gamethis.transform.position).sqrMagnitude;
}
else
{
if (distance > (transform.position - gamethis.transform.position).sqrMagnitude)
{
gamevalue = gamevalue1;
distance = (transform.position - gamethis.transform.position).sqrMagnitude;
}
}
gamevalue1++;
}
print(gamevalue);
lisadd.Add(game[gamevalue].gameObject);
if ((game[gamevalue].transform.position - game[pos].transform.position).sqrMagnitude != 0)
{

distance = 0;
gamevalue = 0;
gamevalue1 = 0;

foreach (GameObject item in lisadd[0].GetComponent<TTTT>().game)//2
{
if (distance == 0)
{
distance = (game[pos].transform.position - item.transform.position).sqrMagnitude;
}
else
{
if (distance > (game[pos].transform.position - item.transform.position).sqrMagnitude)
{
gamevalue = gamevalue1;
distance = (game[pos].transform.position - item.transform.position).sqrMagnitude;
}
}
gamevalue1++;
}
lisadd.Add(lisadd[0].GetComponent<TTTT>().game[gamevalue]);
if ((game[pos].transform.position - lisadd[0].GetComponent<TTTT>().game[gamevalue].transform.position).sqrMagnitude != 0)
{
distance = 0;
gamevalue = 0;
gamevalue1 = 0;


foreach (GameObject item in lisadd[1].GetComponent<TTTT>().game)//3
{
if (distance == 0)
{
distance = (game[pos].transform.position - item.transform.position).sqrMagnitude;
}
else
{
if (distance > (game[pos].transform.position - item.transform.position).sqrMagnitude)
{
gamevalue = gamevalue1;
distance = (game[pos].transform.position - item.transform.position).sqrMagnitude;
}
}
print(gamevalue);
gamevalue1++;
}
lisadd.Add(lisadd[1].GetComponent<TTTT>().game[gamevalue]);
if ((game[pos].transform.position - lisadd[1].GetComponent<TTTT>().game[gamevalue].transform.position).sqrMagnitude != 0)
{
print("第" + lisadd.Count);

distance = 0;
gamevalue = 0;
gamevalue1 = 0;

foreach (GameObject item in lisadd[2].GetComponent<TTTT>().game)//4
{
if (distance == 0)
{
distance = (game[pos].transform.position - item.transform.position).sqrMagnitude;
}
else
{
if (distance > (game[pos].transform.position - item.transform.position).sqrMagnitude)
{
gamevalue = gamevalue1;
distance = (game[pos].transform.position - item.transform.position).sqrMagnitude;
}
}
gamevalue1++;
}
lisadd.Add(lisadd[2].GetComponent<TTTT>().game[gamevalue]);
if ((game[pos].transform.position - lisadd[2].GetComponent<TTTT>().game[gamevalue].transform.position).sqrMagnitude != 0)
{
print("第" + lisadd.Count);

distance = 0;
gamevalue = 0;
gamevalue1 = 0;

foreach (GameObject item in lisadd[3].GetComponent<TTTT>().game)//5
{
if (distance == 0)
{
distance = (game[pos].transform.position - item.transform.position).sqrMagnitude;
}
else
{
if (distance > (game[pos].transform.position - item.transform.position).sqrMagnitude)
{
gamevalue = gamevalue1;
distance = (game[pos].transform.position - item.transform.position).sqrMagnitude;
}
}
gamevalue1++;
}
lisadd.Add(lisadd[3].GetComponent<TTTT>().game[gamevalue]);
if ((game[pos].transform.position - lisadd[2].GetComponent<TTTT>().game[gamevalue].transform.position).sqrMagnitude != 0)
{

}
}
}
}
}
bolon_off = true;
}

}

public void Buttonkool()
{
if (int.Parse(valueinput.text)<game.Length)
{
pos = int.Parse(valueinput.text);
valueinput.text = "";
bol = true;
}
}
}

以上是关于Unity 自动搜索最优路径方式的主要内容,如果未能解决你的问题,请参考以下文章

路径规划基于matlab自动化拣货最优路径含Matlab源码 1713期

A*路径搜索算法基于A星的最优避障路径搜索算法的MATLAB仿真+GUI界面

递归,搜索,回溯,最优路径(线段)

算法 | A*算法实现最优路径规划

Unity资源加载路径及加载方式小结

A*算法导航网格路径点寻路对比(A-Star VS NavMesh VS WayPoint)