zoukankan      html  css  js  c++  java
  • 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;
    }
    }
    }

  • 相关阅读:
    小程序支付
    小程序传参
    git
    学习过程遇到问题的解决方法
    进程创建注意事项:函数及对应包
    进程创建:函数及对应包
    mac解决eclipse 端口占用问题
    暴力
    doc2vec
    Java正则表达式的解释说明
  • 原文地址:https://www.cnblogs.com/suiyi78/p/12989870.html
Copyright © 2011-2022 走看看