首页 > 编程语言 > 详细

Unity 自动搜索最优路径方式

时间:2020-05-29 23:19:56      阅读:63      评论:0      收藏:0      [点我收藏+]

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


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 自动搜索最优路径方式

原文:https://www.cnblogs.com/suiyi78/p/12989870.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!