Beispiel #1
0
    void SetTargetPosition()
    {
        //Plane plane = new Plane(Vector3.up, transform.position);
        RaycastHit hit;
        Ray        rayFromCamera = Camera.main.ScreenPointToRay(Input.mousePosition);

        //float point = 0f;

        if (true == (Physics.Raycast(rayFromCamera.origin, rayFromCamera.direction * 10, out hit)))
        {
            Vector3    gridPoint           = Vector3Util.GridVector(hit.point); // 이동해야할 위치의 x, z좌표
            Collider   selectedObjCollider = hit.collider;
            GameObject obj = hit.collider.gameObject;

            gridPosition = gridPoint;

            {
                int j = 1;
                for (int i = 0; i < 10; i++)
                {
                    Ray rayDown = new Ray(gridPoint + Vector3.up * j / 2, Vector3.down * j);

                    if (selectedObjCollider.Raycast(rayDown, out hit, j))
                    //if (Physics.Raycast(rayDown, out hit)) // 위로 광선을 쐈는데 터치된 오브젝트에 닿음
                    {
                        targetPosition = hit.point;
                    }


                    j *= 2;
                }
            }

            Vector3 a = targetPosition;

            if (!pathFindingTest)
            {
                unitPathFinder.goTo(targetPosition);
            }
            else
            {
                GetComponent <CorvoPathFinder>().findPath(targetPosition);
            }
        }
    }
Beispiel #2
0
    IEnumerator generatingGrid()
    {
        haveGrid   = false;
        generating = true;

        RaycastHit _hit;
        //crea griglia di punti

        int passibase = 0, maxPassi = setMaxPassi();
        //MIA VERSIONE
        double _startTime = Time.time;

        bool _diagonal = false;

        for (int lev = 0; lev < gridLevels; lev++)
        {
            Vector3 _up = Vector3.down * gridLevels / 2 * levelHeight + Vector3.up * (lev + 1) * levelHeight;
            for (int i = 0; i < sX; i++)     //RIGHE
            {
                for (int j = 0; j < sY; j++) //COLONNE
                {
                    passibase++;
                    grid[i, j, lev] = null;
                    Vector3 _startPos = Vector3Util.GridVector(Vector3Util.GridVector(transform.position) + Vector3.right * (i - (sX / 2)) * nodeWidth + Vector3.forward * (j - (sY / 2)) * nodeWidth);

                    if (Physics.Raycast(_startPos + _up, -Vector3.up, out _hit, levelHeight * 1.3f, walkMask))
                    {
                        if (prohibiteMask == (prohibiteMask | (1 << _hit.collider.gameObject.layer)))
                        {
                            continue;
                        }
                        if (_hit.collider.transform.root == transform.root)
                        {
                            continue;
                        }

                        switch (diagonals)
                        {
                        case diagonalPath.everywhere:
                            _diagonal = true;
                            break;

                        case diagonalPath.nowhere:
                            _diagonal = false;
                            break;
                        }

                        TestVector(_hit.point);

                        if (lev == 0)
                        {
                            grid[i, j, lev] = new GridNode(_hit.point);
                        }
                        else
                        {
                            if (grid[i, j, lev - 1] != null)
                            {
                                if (_hit.point.y - grid[i, j, lev - 1].getPosition().y > nodeWidth / 3)
                                {
                                    grid[i, j, lev] = new GridNode(_hit.point);
                                }
                                else
                                {
                                    continue;
                                }
                            }
                            else
                            {
                                grid[i, j, lev] = new GridNode(_hit.point);
                            }
                        }

                        if (j > 0) // 왼쪽이 아니야.
                        {
                            if (_diagonal)
                            {
                                if (i > 0) // 높이 올라가지 않는다.
                                {
                                    //ALTO SINISTRA
                                    conectGridPoints(grid[i, j, lev], grid[i - 1, j - 1, lev]);
                                    if (lev > 0)
                                    {
                                        conectGridPoints(grid[i, j, lev], grid[i - 1, j - 1, lev - 1]);
                                        if (i < sX - 1 && j < sY - 1)
                                        {
                                            conectGridPoints(grid[i, j, lev], grid[i + 1, j + 1, lev - 1]);
                                        }
                                    }
                                }
                            }

                            // 높음
                            conectGridPoints(grid[i, j, lev], grid[i, j - 1, lev]);
                            if (lev > 0)
                            {
                                conectGridPoints(grid[i, j, lev], grid[i, j - 1, lev - 1]);
                                if (j < sY - 1)
                                {
                                    conectGridPoints(grid[i, j, lev], grid[i, j + 1, lev - 1]);
                                }
                            }
                        }
                        if (i > 0)// 그것을 극복하지 못한다.
                        {
                            if (_diagonal)
                            {
                                if (j < sY - 1)//그것은 오른쪽으로 오지 않는다.
                                {
                                    //왼쪽 하단
                                    conectGridPoints(grid[i, j, lev], grid[i - 1, j + 1, lev]);
                                    if (lev > 0)
                                    {
                                        conectGridPoints(grid[i, j, lev], grid[i - 1, j + 1, lev - 1]);
                                        if (i < sX - 1 && j > 0)
                                        {
                                            conectGridPoints(grid[i, j, lev], grid[i + 1, j - 1, lev - 1]);
                                        }
                                    }
                                }
                            }
                            //SINISTRA
                            conectGridPoints(grid[i, j, lev], grid[i - 1, j, lev]);
                            if (lev > 0)
                            {
                                conectGridPoints(grid[i, j, lev], grid[i - 1, j, lev - 1]);
                                if (i < sX - 1)
                                {
                                    conectGridPoints(grid[i, j, lev], grid[i + 1, j, lev - 1]);
                                }
                            }
                        }
                        //STOPPA PER AZIONI
                        passibase += 4;
                        //CHECK E' ABBASTANZA VICINO
                    }

                    if (passibase > maxPassi)
                    {
                        passibase = 0;
                        if (Application.isPlaying)
                        {
                            yield return(null);
                        }

                        if (!generating)                        //forced stop
                        {
                            yield break;
                        }
                    }
                }
            }
        }


        if (!Application.isPlaying)
        {
            print("Grid generated. Time needed: " + (Time.time - _startTime));
        }
        generating = false;
        haveGrid   = true;
    }