仿真物流仓库拣货车拣货

一:S型路径

1、预设货架八行四列,加上每列startPos和endPos,一共十行四列
白色路径点,红色小车
2、给路径点改名字,便于随机和查找
S和E分别代表每一列的头和尾
3、先从32个点中随机出来10个点,要求不能重复,将随机出来的点存储到数组里面

public void SuiJiSecond()
{
        //随机出来的目标点数组
        List<Transform> tempList = new List<Transform>();
            //随机
            for (int i = 0; i < 10; i++)
            {
                GetName();

                Transform tran = Points.Find(name);

                tempList.Add(tran);

                nameList.Add(name);
            }

            Debug.Log("temp:" + tempList.Count);
        }
void GetName()
        {
            int line = Random.Range(1, 5);
            int row = Random.Range(1, 9);

            name = "p" + line + "-" + row;

            if (line < 10)
            {
                name = "p0" + line + "-" + row;
            }
            else
            {
                name = "p" + line + "-" + row;
            }
            if (nameList.Contains(name))
            {
                GetName();
            }
        }

4、接下来进行排序,S型路径要求1s—>1e—>2e—>2s—>3s—>3e—>4e—>4s—>初始点;如果某一列没有目标点,则直接跳过

 //一层排序--列的顺序从小到大排
            //一层排序后的目标数组
            List<Transform> tempList2 = new List<Transform>();
            for (int j = 0; j < 5; j++)
            {
                for (int i = 0; i < tempList.Count; i++)
                {
                    Transform tran1 = tempList[i];
                    string tran1name = tran1.name.Substring(1, 2);
                    int num = int.Parse(tran1name);

                    if (num <= j)
                    {
                        if (tempList2.Contains(tran1))
                        {
                            continue;
                        }
                        else
                        {
                            tempList2.Add(tran1);
                        }
                    }
                }
            }
            Debug.Log("tempList2:" + tempList2.Count);
//二层排序--通过判断具体有第几列的随机数,控制a的值。
            //targetList 为最终的目标数组
            List<Transform> targetList = new List<Transform>();
            for (int j = 1; j < 5; j++)
            {
                kaifang = true;
                for (int i = 0; i < tempList2.Count; i++)
                {
                    Transform tran2 = tempList2[i];
                    string tran2name = tran2.name.Substring(1, 2);
                    int num = int.Parse(tran2name);
                    if (num == j)
                    {
                        if (kaifang)
                        {
                            kaifang = false;
                            a++;
                        }
                        tempList3.Add(tran2);
                    }
                    else { continue; }
                }
                ErCengPaiXu();
            }
            Debug.Log("targetList:" + targetList.Count);
            for (int i = 0; i < targetList.Count; i++)
            {
                string tname = targetList[i].name;
                Debug.Log("name:" + tname);
            }

            TargetModel = targetList[i];
/// <summary>
        /// a为奇数正着排,a为偶数倒着排
        /// </summary>
        public void ErCengPaiXu()
        {
            if (a % 2 == 1)
            {
                for (int m = 1; m < 9; m++)
                {
                    for (int n = 0; n < tempList3.Count; n++)
                    {
                        Transform tran3 = tempList3[n];
                        string tran3name = tran3.name.Substring(4, 1);
                        int num = int.Parse(tran3name);
                        Debug.Log(num);
                        if (num <= m)
                        {
                            if (targetList.Contains(tran3))
                            {
                                continue;
                            }
                            else
                            {
                                Debug.Log(num);
                                Debug.Log("tran3name" + tran3.name);
                                targetList.Add(tran3);
                            }
                        }
                    }
                }
            }
            else if (a % 2 == 0)
            {
                for (int m = 8; m > 0; m--)
                {
                    for (int n = 0; n < tempList3.Count; n++)
                    {
                        Transform tran3 = tempList3[n];
                        string tran3name = tran3.name.Substring(4, 1);
                        int num = int.Parse(tran3name);
                        Debug.Log(num);
                        if (num >= m)
                        {
                            if (targetList.Contains(tran3))
                            {
                                continue;
                            }
                            else
                            {
                                Debug.Log(num);
                                Debug.Log("tran3name" + tran3.name);
                                targetList.Add(tran3);
                            }
                        }
                    }
                }
            }
            tempList3.Clear();
        }

5、然后设置路径的添加与删除

//S型
        public List<Transform> GetSecondPath()
        {
            Lujing.Clear();

            if (SourceModel == null)
            {
                string pstartName = TargetModel.name.Substring(0, 3);//p05-11
                Debug.Log("pstartName:" + pstartName);
                Transform ps = Points.Find(pstartName + "s");
                Lujing.Add(ps);
                Lujing.Add(TargetModel);
            }
            else
            {
                string psourceName = SourceModel.name.Substring(0, 3);//p05-11
                string ptargetName = TargetModel.name.Substring(0, 3);//p05-11
                Transform pe1 = Points.Find(psourceName + "s");
                Transform pe2 = Points.Find(ptargetName + "s");
                Transform pe3 = Points.Find(psourceName + "e");
                Transform pe4 = Points.Find(ptargetName + "e");
                if (psourceName == ptargetName)
                {
                    Lujing.Add(TargetModel);
                }
                else
                {
                    b++;
                    if (b % 2 == 1)
                    {
                        Lujing.Add(pe3);
                        Lujing.Add(pe4);
                        Lujing.Add(TargetModel);
                    }
                    else if (b % 2 == 0)
                    {
                        Lujing.Add(pe1);
                        Lujing.Add(pe2);
                        Lujing.Add(TargetModel);
                    }
                    Debug.Log("b="+b);
                }
            }

            return Lujing;
        }

6、最后设置小车的移动

void Update()
        {
            if (IsRun)
            {
                Transform endObj = Lujing[0];
                Vector3 direction = endObj.position - transform.position;

                transform.position += direction.normalized * Speed * Time.deltaTime;
                transform.LookAt(endObj);
                //timer += Time.deltaTime;

                float dis = Vector3.Distance(transform.position, endObj.position);
                if (dis < 0.5f)
                {
                    Lujing.RemoveAt(0);
                }

                if (Lujing.Count == 0)
                {
                    IsRun = false;
                    Debug.Log("eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee:" + timer);
                    SourceModel = TargetModel;
                    transform.position = TargetModel.position;

                    StartCoroutine(Change());
                }
            }
        }
        IEnumerator Change()
        {
            yield return new WaitForSeconds(2);
            if (i < targetList.Count - 1)
            {
                i++;
                TargetModel = targetList[i];
                //GetFirstPath();
                GetSecondPath();
                //GetThirdPath();
                IsRun = true;
            }
        }

中点型路径

要求,每列先捡前一半,再捡后一半。s—>s—>s—>s—>e—>e—>e—>e—>s—>初始位置,某列没有则跳过

返回型

要求每列捡完后从下一列的s点开始,不需要e点。1s—>1s—>2s—>2s—>3s—>3s—>4s—>4s—>初始点,某列没有则跳过
算法与s型路径类似,不添加e点即可

下面完整代码

public class LuXian : MonoBehaviour 
	{
        public bool IsRun = false;
        float Speed = 10;
        public static Transform SourceModel;
        public Transform TargetModel;

        public List<Transform> Lujing = new List<Transform>();

        List<Transform> targetList = new List<Transform>();

        List<string> nameList = new List<string>();

        public Transform Points;
        float timer = 0;

        List<Transform> tempList = new List<Transform>();

        List<Transform> tempList2 = new List<Transform>();

        List<Transform> tempList3 = new List<Transform>();

        public int i = 0;

        //List<int> numBuf = new List<int>();
        string name = "";

        bool kaifang = false;
        int a = 0;
        int b = 0;
        /// <summary>
        /// Unity Method
        /// </summary>
        void Start()
        {
            //SuiJiFirst();
            //GetFirstPath();

            SuiJiSecond();
            GetSecondPath();

            //SuiJiThird();
            //GetThirdPath();
            IsRun = true;
            //CeShi();
        }

        public void SuiJiFirst()
        {
            List<Transform> tempList = new List<Transform>();

            List<Transform> tempList2 = new List<Transform>();
            //随机
            for (int i = 0; i < 5; i++)
            {
                GetName();

                Transform tran = Points.Find(name);

                tempList.Add(tran);

                nameList.Add(name);
            }

            Debug.Log("temp:" + tempList.Count);

            //一层排序
            for (int j = 0; j < 5; j++)
            {
                for (int i = 0; i < tempList.Count; i++)
                {
                    Transform tran1 = tempList[i];
                    string tran1name = tran1.name.Substring(4, 1);
                    int num = int.Parse(tran1name);

                    if (num <= j)
                    {
                        if (tempList2.Contains(tran1))
                        {
                            continue;
                        }
                        else
                        {
                            tempList2.Add(tran1);
                        }
                    }
                }
            }

            二层排序
            for (int j = 0; j < 5; j++)
            {
                for (int i = 0; i < tempList2.Count; i++)
                {
                    Transform tran = tempList2[i];
                    string tranname = tran.name.Substring(1, 2);
                    int num = int.Parse(tranname);

                    if (num <= j)
                    {
                        if (targetList.Contains(tran))
                        {
                            continue;
                        }
                        else
                        {
                            targetList.Add(tran);
                        }
                    }
                }
            }

            Debug.Log("targetList:" + targetList.Count);
            for (int i = 0; i < targetList.Count; i++)
            {
                string tname = targetList[i].name;
                Debug.Log("name:" + tname);
            }

            TargetModel = targetList[i];
        }

        public void SuiJiSecond()
        {
            //随机
            for (int i = 0; i < 10; i++)
            {
                GetName();

                Transform tran = Points.Find(name);

                tempList.Add(tran);

                nameList.Add(name);
            }

            Debug.Log("temp:" + tempList.Count);

            //一层排序--列的顺序从小到大排
            for (int j = 0; j < 5; j++)
            {
                for (int i = 0; i < tempList.Count; i++)
                {
                    Transform tran1 = tempList[i];
                    string tran1name = tran1.name.Substring(1, 2);
                    int num = int.Parse(tran1name);

                    if (num <= j)
                    {
                        if (tempList2.Contains(tran1))
                        {
                            continue;
                        }
                        else
                        {
                            tempList2.Add(tran1);
                        }
                    }
                }
            }
            Debug.Log("tempList2:" + tempList2.Count);
            //for (int i = 0; i < tempList2.Count; i++)
            //{
            //    string tname = tempList2[i].name;
            //    Debug.Log("name:" + tname);
            //}

            //二层排序--通过判断具体有第几列的随机数,控制a的值。
            for (int j = 1; j < 5; j++)
            {
                kaifang = true;
                for (int i = 0; i < tempList2.Count; i++)
                {
                    Transform tran2 = tempList2[i];
                    string tran2name = tran2.name.Substring(1, 2);
                    int num = int.Parse(tran2name);
                    if (num == j)
                    {
                        if (kaifang)
                        {
                            kaifang = false;
                            a++;
                        }
                        tempList3.Add(tran2);
                    }
                    else { continue; }
                }

                //Debug.Log("tempList3:" + tempList3.Count);
                //for (int i = 0; i < tempList3.Count; i++)
                //{
                //    string tname = tempList3[i].name;
                //    Debug.Log("tempList3name:" + tname);
                //}
                ErCengPaiXu();
#region
                //if (a % 2 == 1)
                //{
                //    for (int k = 0; k < 5; k++)
                //    {
                //        for (int l = 0; l < tempList3.Count; l++)
                //        {
                //            Transform tran3 = tempList3[l];
                //            string tran3name = tran3.name.Substring(4, 1);
                //            int num = int.Parse(tran3name);
                //            Debug.Log(111111111111111111);
                //            if (num <= k)
                //            {
                //                Debug.Log(22222222222222222);
                //                if (targetList.Contains(tran3))
                //                {
                //                    Debug.Log("aaaaaaaaaaa");
                //                    continue;
                //                }
                //                else
                //                {
                //                    targetList.Add(tran3);
                //                    Debug.Log(333333333333333);
                //                }
                //            }
                //        }
                //    }
                //}
                //else if (a % 2 == 0)
                //{
                //    for (int k = 4; k > 0; k--)
                //    {
                //        for (int l = 0; l < tempList3.Count; l++)
                //        {
                //            Transform tran3 = tempList3[l];
                //            string tran3name = tran3.name.Substring(4, 1);
                //            int num = int.Parse(tran3name);
                //            Debug.Log(4444444444444444);
                //            if (num <= k)
                //            {
                //                Debug.Log(555555555555);
                //                if (targetList.Contains(tran3))
                //                {
                //                    Debug.Log("bbbbbbbbbbbbbbbb");
                //                    continue;
                //                }
                //                else
                //                {
                //                    targetList.Add(tran3);
                //                    Debug.Log(66666666666666);
                //                }
                //            }
                //        }
                //    }
                //}

                #endregion
            }

            Debug.Log("targetList:" + targetList.Count);
            for (int i = 0; i < targetList.Count; i++)
            {
                string tname = targetList[i].name;
                Debug.Log("name:" + tname);
            }

            TargetModel = targetList[i];
        }

        /// <summary>
        /// a为奇数正着排,a为偶数倒着排
        /// </summary>
        public void ErCengPaiXu()
        {
            if (a % 2 == 1)
            {
                for (int m = 1; m < 9; m++)
                {
                    for (int n = 0; n < tempList3.Count; n++)
                    {
                        Transform tran3 = tempList3[n];
                        string tran3name = tran3.name.Substring(4, 1);
                        int num = int.Parse(tran3name);
                        Debug.Log(num);
                        if (num <= m)
                        {
                            if (targetList.Contains(tran3))
                            {
                                continue;
                            }
                            else
                            {
                                Debug.Log(num);
                                Debug.Log("tran3name" + tran3.name);
                                targetList.Add(tran3);
                            }
                        }
                    }
                }
            }
            else if (a % 2 == 0)
            {
                for (int m = 8; m > 0; m--)
                {
                    for (int n = 0; n < tempList3.Count; n++)
                    {
                        Transform tran3 = tempList3[n];
                        string tran3name = tran3.name.Substring(4, 1);
                        int num = int.Parse(tran3name);
                        Debug.Log(num);
                        if (num >= m)
                        {
                            if (targetList.Contains(tran3))
                            {
                                continue;
                            }
                            else
                            {
                                Debug.Log(num);
                                Debug.Log("tran3name" + tran3.name);
                                targetList.Add(tran3);
                            }
                        }
                    }
                }
            }
            tempList3.Clear();
        }

        public void SuiJiThird()
        {
            //随机
            for (int i = 0; i < 20; i++)
            {
                GetName();

                Transform tran = Points.Find(name);

                tempList.Add(tran);

                nameList.Add(name);
            }

            //Debug.Log("tempList:" + tempList.Count);
            //for (int i = 0; i < tempList.Count; i++)
            //{
            //    string tname = tempList[i].name;
            //    Debug.Log("tempListname:" + tname);
            //}

            //第一种
            #region 
            //第一层排序
            for (int j = 0; j < 9; j++)
            {
                for (int i = 0; i < tempList.Count; i++)
                {
                    Transform tran1 = tempList[i];
                    string tran1name = tran1.name.Substring(4, 1);
                    int num = int.Parse(tran1name);

                    if (num <= j)
                    {
                        if (tempList2.Contains(tran1))
                        {
                            continue;
                        }
                        else
                        {
                            tempList2.Add(tran1);
                        }
                    }
                }
            }

            Debug.Log("tempList2:" + tempList2.Count);
            for (int i = 0; i < tempList2.Count; i++)
            {
                string tempList2name = tempList2[i].name;
                Debug.Log("tempList2name:" + tempList2name);
            }

            //第一段
            for (int i = 0; i < tempList2.Count; i++)
            {
                Transform tran2 = tempList2[i];
                string tran2name = tran2.name.Substring(4, 1);
                int num = int.Parse(tran2name);
                if (num < 5)
                {
                    tempList3.Add(tran2);
                }
            }
            for (int m = 0; m < 5; m++)
            {
                for (int n = 0; n < tempList3.Count; n++)
                {
                    Transform tran3 = tempList3[n];
                    string tran3name = tran3.name.Substring(1, 2);
                    int num = int.Parse(tran3name);
                    if (num <= m)
                    {
                        if (targetList.Contains(tran3))
                        {
                            continue;
                        }
                        else
                        {
                            targetList.Add(tran3);
                        }
                    }
                }
            }
            //Debug.Log("targetList:" + targetList.Count);
            //for (int i = 0; i < targetList.Count; i++)
            //{
            //    string tname = targetList[i].name;
            //    Debug.Log("name:" + tname);
            //}
            tempList3.Clear();

            //第二段
            for (int i = 0; i < tempList2.Count; i++)
            {
                Transform tran2 = tempList2[i];
                string tran2name = tran2.name.Substring(4, 1);
                int num = int.Parse(tran2name);
                if (num >= 5)
                {
                    tempList3.Add(tran2);
                }
            }
            for (int m = 5; m > 0; m--)
            {
                for (int n = 0; n < tempList3.Count; n++)
                {
                    Transform tran3 = tempList3[n];
                    string tran3name = tran3.name.Substring(1, 2);
                    int num = int.Parse(tran3name);
                    if (num >= m)
                    {
                        if (targetList.Contains(tran3))
                        {
                            continue;
                        }
                        else
                        {
                            targetList.Add(tran3);
                        }
                    }
                }
            }
            tempList3.Clear();
            Debug.Log("targetList:" + targetList.Count);
            for (int i = 0; i < targetList.Count; i++)
            {
                string tname = targetList[i].name;
                Debug.Log("name:" + tname);
            }
            #endregion

            //第二种
            #region
            第一段排序
            //for (int i = 0; i < tempList.Count; i++)
            //{
            //    Transform tran1 = tempList[i];
            //    string tran1name = tran1.name.Substring(4, 1);
            //    int num = int.Parse(tran1name);
            //    if (num < 5)
            //    {
            //        tempList2.Add(tran1);
            //    }
            //}

            第一段第一层
            //for (int i = 1; i < 9; i++)
            //{
            //    for (int j = 0; j < tempList2.Count; j++)
            //    {
            //        Transform tran2 = tempList2[j];
            //        string tran2name = tran2.name.Substring(4, 1);
            //        int num = int.Parse(tran2name);
            //        if (num <= i)
            //        {
            //            if (tempList3.Contains(tran2))
            //            {
            //                continue;
            //            }
            //            else
            //            {
            //                tempList3.Add(tran2);
            //            }
            //        }
            //    }
            //}
            第一段第二层
            //for (int m = 1; m < 5; m++)
            //{
            //    for (int n = 0; n < tempList3.Count; n++)
            //    {
            //        Transform tran3 = tempList3[n];
            //        string tran3name = tran3.name.Substring(1, 2);
            //        int num = int.Parse(tran3name);
            //        if (num <= m)
            //        {
            //            if (targetList.Contains(tran3))
            //            {
            //                continue;
            //            }
            //            else
            //            {
            //                targetList.Add(tran3);
            //            }
            //        }
            //    }
            //}
            //tempList2.Clear();
            //tempList3.Clear();

            第二段排序
            //for (int i = 0; i < tempList.Count; i++)
            //{
            //    Transform tran1 = tempList[i];
            //    string tran1name = tran1.name.Substring(4, 1);
            //    int num = int.Parse(tran1name);
            //    if (num >= 5)
            //    {
            //        tempList2.Add(tran1);
            //    }
            //}

            第二段第一层
            //for (int i = 1; i < 9; i++)
            //{
            //    for (int j = 0; j < tempList2.Count; j++)
            //    {
            //        Transform tran2 = tempList2[j];
            //        string tran2name = tran2.name.Substring(4, 1);
            //        int num = int.Parse(tran2name);
            //        if (num <= i)
            //        {
            //            if (tempList3.Contains(tran2))
            //            {
            //                continue;
            //            }
            //            else
            //            {
            //                tempList3.Add(tran2);
            //            }
            //        }
            //    }
            //}

            第二段第二层
            //for (int m = 5; m > 0; m--)
            //{
            //    for (int n = 0; n < tempList3.Count; n++)
            //    {
            //        Transform tran3 = tempList3[n];
            //        string tran3name = tran3.name.Substring(1, 2);
            //        int num = int.Parse(tran3name);
            //        if (num >= m)
            //        {
            //            if (targetList.Contains(tran3))
            //            {
            //                continue;
            //            }
            //            else
            //            {
            //                Debug.Log("11111111");
            //                targetList.Add(tran3);
            //            }
            //        }
            //    }
            //}
            //tempList2.Clear();
            //tempList3.Clear();

            //Debug.Log("targetList:" + targetList.Count);
            //for (int i = 0; i < targetList.Count; i++)
            //{
            //    string tname = targetList[i].name;
            //    Debug.Log("name:" + tname);
            //}
            #endregion


            TargetModel = targetList[i];
        }


        void GetName()
        {
            int line = Random.Range(1, 5);
            int row = Random.Range(1, 9);

            name = "p" + line + "-" + row;

            if (line < 10)
            {
                name = "p0" + line + "-" + row;
            }
            else
            {
                name = "p" + line + "-" + row;
            }
            if (nameList.Contains(name))
            {
                GetName();
            }
        }

        public List<Transform> GetFirstPath()
        {
            Lujing.Clear();

            if (SourceModel == null)
            {
                string pstartName = TargetModel.name.Substring(0, 3);//p05-11
                Debug.Log("pstartName:" + pstartName);
                Transform ps = Points.Find(pstartName + "s");
                Lujing.Add(ps);
                Lujing.Add(TargetModel);
            }
            else
            {
                string psourceName = SourceModel.name.Substring(0, 3);//p05-11
                string ptargetName = TargetModel.name.Substring(0, 3);//p05-11
                Transform pe1 = Points.Find(psourceName + "s");
                Transform pe2 = Points.Find(ptargetName + "s");
                if (psourceName == ptargetName)
                {
                    Lujing.Add(TargetModel);
                }
                else
                {
                    Lujing.Add(pe1);
                    Lujing.Add(pe2);
                    Lujing.Add(TargetModel);
                }
            }
            return Lujing;
        }

        //S型
        public List<Transform> GetSecondPath()
        {
            Lujing.Clear();

            if (SourceModel == null)
            {
                string pstartName = TargetModel.name.Substring(0, 3);//p05-11
                Debug.Log("pstartName:" + pstartName);
                Transform ps = Points.Find(pstartName + "s");
                Lujing.Add(ps);
                Lujing.Add(TargetModel);
            }
            else
            {
                string psourceName = SourceModel.name.Substring(0, 3);//p05-11
                string ptargetName = TargetModel.name.Substring(0, 3);//p05-11
                Transform pe1 = Points.Find(psourceName + "s");
                Transform pe2 = Points.Find(ptargetName + "s");
                Transform pe3 = Points.Find(psourceName + "e");
                Transform pe4 = Points.Find(ptargetName + "e");
                if (psourceName == ptargetName)
                {
                    Lujing.Add(TargetModel);
                }
                else
                {
                    b++;
                    if (b % 2 == 1)
                    {
                        Lujing.Add(pe3);
                        Lujing.Add(pe4);
                        Lujing.Add(TargetModel);
                    }
                    else if (b % 2 == 0)
                    {
                        Lujing.Add(pe1);
                        Lujing.Add(pe2);
                        Lujing.Add(TargetModel);
                    }
                    Debug.Log("b="+b);
                }
            }

            return Lujing;
        }

        //中点型
        public List<Transform> GetThirdPath()
        {
            Lujing.Clear();

            if (SourceModel == null)
            {
                string pstartName = TargetModel.name.Substring(0, 3);//p05-11
                Debug.Log("pstartName:" + pstartName);
                Transform ps = Points.Find(pstartName + "s");
                Lujing.Add(ps);
                Lujing.Add(TargetModel);
            }
            else
            {
                
                string psourceName = SourceModel.name.Substring(0, 3);//p05-11
                string ptargetName = TargetModel.name.Substring(0, 3);//p05-11
                int ptargetNum = int.Parse(TargetModel.name.Substring(4, 1));
                int Snum = int.Parse(SourceModel.name.Substring(1, 2));
                int Tnum = int.Parse(TargetModel.name.Substring(1, 2));
                Transform pe1 = Points.Find(psourceName + "s");
                Transform pe2 = Points.Find(ptargetName + "s");
                Transform pe3 = Points.Find(psourceName + "e");
                Transform pe4 = Points.Find(ptargetName + "e");

                if (ptargetNum < 5)
                {
                    if (psourceName == ptargetName)
                    {
                        Lujing.Add(TargetModel);
                    }
                    else
                    {
                        Lujing.Add(pe1);
                        Lujing.Add(pe2);
                        Lujing.Add(TargetModel);
                    }
                }
                else
                {
                    if (psourceName == ptargetName)
                    {
                        Lujing.Add(TargetModel);
                    }
                    else 
                    {
                        if (Snum < Tnum)
                        {
                            Lujing.Add(pe1);
                            Lujing.Add(pe2);
                            Lujing.Add(TargetModel);
                        }
                        else
                        {
                            Lujing.Add(pe3);
                            Lujing.Add(pe4);
                            Lujing.Add(TargetModel);
                        }
                    }
                }
            }

            return Lujing;
        }

        /// <summary>
        /// Unity Method
        /// </summary>
        void Update()
        {
            if (IsRun)
            {
                Transform endObj = Lujing[0];
                Vector3 direction = endObj.position - transform.position;

                transform.position += direction.normalized * Speed * Time.deltaTime;
                transform.LookAt(endObj);
                //timer += Time.deltaTime;

                float dis = Vector3.Distance(transform.position, endObj.position);
                if (dis < 0.5f)
                {
                    Lujing.RemoveAt(0);
                }

                if (Lujing.Count == 0)
                {
                    IsRun = false;
                    Debug.Log("eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee:" + timer);
                    SourceModel = TargetModel;
                    transform.position = TargetModel.position;

                    StartCoroutine(Change());
                }
            }

        }

        IEnumerator Change()
        {
            yield return new WaitForSeconds(2);
            if (i < targetList.Count - 1)
            {
                i++;
                TargetModel = targetList[i];
                //GetFirstPath();
                GetSecondPath();
                //GetThirdPath();
                IsRun = true;
            }
        }
    }

需要模型拣货的过程,只需要在到达下一路径点时,写出对应的功能即可,如下

void Update()
        {
            if (IsRun)
            {
                Transform endObj = Lujing[0];
                Vector3 direction = endObj.position - transform.position;

                transform.position += direction.normalized * Speed * Time.deltaTime;
                transform.LookAt(endObj);
                //timer += Time.deltaTime;

                float dis = Vector3.Distance(transform.position, endObj.position);
                if (dis < 1f)
                {
                    Lujing.RemoveAt(0);
                }

                if (Lujing.Count == 0)
                {
                    IsRun = false;
                    //Debug.Log("eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee:" + timer);
                    SourceModel = TargetModel;
                    transform.position = TargetModel.position;

                    if (TargetModel == StartPos)
                    {
                        StartCoroutine(TuiChu());
                    }
                    else
                    {
                        transform.LookAt(new Vector3(targetWuTi.position.x, transform.position.y, targetWuTi.position.z));
                        StartCoroutine(PlayAnim());
                    }
                    
                }
            }

            if (JiShi)
            {
                timer += Time.deltaTime;
                time.text = timer.ToString("f1") + "s";
                count.text = num.ToString();
            }
        }

        IEnumerator TuiChu()
        {
            yield return new WaitForSeconds(0.1f);
            JiShi = false;
            TiShi.gameObject.SetActive(true);
            TimeText.text = time.text;
        }

        IEnumerator PlayAnim()
        {
            if (i < targetList.Count - 1)
            {
                Zhou5.transform.DOLocalMoveX(-0.292f, 1);
                yield return new WaitForSeconds(1);
                targetWuTi.GetChild(0).gameObject.SetActive(false);
                GameObject go = Instantiate(Resources.Load<GameObject>("wuti"), Zhou5.transform);
                yield return new WaitForSeconds(0.2f);
                Zhou5.transform.DOLocalMoveX(0.42f, 1);
                yield return new WaitForSeconds(1);
                Zhou6.transform.DOLocalMoveY((0.1f * i + 0.32f), 2);
                yield return new WaitForSeconds(2.1f);
                go.transform.SetParent(transform);
                yield return new WaitForSeconds(0.1f);
                Zhou6.transform.DOLocalMoveY(3.5f, 2);
                yield return new WaitForSeconds(2.1f);
                num++;
                i++;
                TargetModel = targetList[i];
                targetWuTi = wuti[i];
                GetFirstPath();
                IsRun = true;
            }
            else
            {
                Zhou5.transform.DOLocalMoveX(-0.292f, 1);
                yield return new WaitForSeconds(1);
                targetWuTi.GetChild(0).gameObject.SetActive(false);
                GameObject go = Instantiate(Resources.Load<GameObject>("wuti"), Zhou5.transform);
                yield return new WaitForSeconds(0.2f);
                Zhou5.transform.DOLocalMoveX(0.42f, 1);
                yield return new WaitForSeconds(1);
                Zhou6.transform.DOLocalMoveY((0.1f * i + 0.32f), 2);
                yield return new WaitForSeconds(2.1f);
                go.transform.SetParent(transform);
                yield return new WaitForSeconds(0.1f);
                Zhou6.transform.DOLocalMoveY(3.5f, 2);
                yield return new WaitForSeconds(2.1f);

                num++;
                TargetModel = StartPos;
                
                GetFirstPath();
                IsRun = true;
            }
        }
  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值