コード例 #1
0
        public void SetAroundPoint(Vector3 point, Vector3 buildPoint)
        {
            state       = UnitPathState.Around;
            aroundPoint = point;
            Vector3 d1 = point - buildPoint;
            Vector3 d2 = transform.position - buildPoint;
            float   d  = Vector3.Dot(d1.normalized, d2.normalized);

//			Debug.Log("SetAroundPoint point=" + point + "  d=" + d);

            Vector3[] points = null;
            if (d < 0)
            {
                Vector3 inPoint = HMath.IntersectionPoint(buildPoint, transform.position, point);
                Vector3 p       = buildPoint + (inPoint - buildPoint).normalized * aroundRadius;
                points = new Vector3[] { transform.position, p, point };
            }
            else
            {
                points = new Vector3[] { transform.position, point };
            }
            aroundPoints = points;


//			m_steeringAgent.SteerAlongPath(points, m_pathAgent.PathManager.PathTerrain );
            m_steeringAgent.SteerAlongPath(points, null);
        }
コード例 #2
0
        /** 检测建筑障碍 */
        public void CheckBuild()
        {
            if (paths.Count == 0)
            {
                return;
            }

            int listCount = paths[0].list.Count;

            for (int i = 0; i < listCount; i++)
            {
                bool       hasHit         = false;
                float      minHitDistance = 9999;
                RaycastHit hitInfo        = new RaycastHit();

                float   radius = 1f;
                Vector3 pos    = Vector3.zero;

                // 查找最近的碰撞体
                foreach (Path path in paths)
                {
                    List <Vector3> list   = path.list;
                    List <Vector3> points = path.points;
                    path.hit = null;

                    points.Add(list[i]);
                    if (i == 0)
                    {
                        continue;
                    }



                    if (i < listCount - 2)
                    {
                        Vector3 p1 = list[i];
                        Vector3 p2 = list[i + 1];


                        RaycastHit _hitInfo;
                        if (Physics.Linecast(p1, p2, out _hitInfo, buildLayer.value))
                        {
                            path.hit = _hitInfo.collider.gameObject;
                            pos      = _hitInfo.collider.transform.position;
                            float hitDistance = Vector3.Distance(paths[0].list[i], pos);

                            radius = (_hitInfo.collider as SphereCollider).radius * _hitInfo.collider.transform.lossyScale.x;
                            if (hitDistance <= radius * hitRadiusMultiply || Vector3.Distance(paths[0].list[i + 1], pos) <= radius * hitRadiusMultiply)
                            {
                                continue;
                            }

                            if (hasHit == false)
                            {
                                hasHit         = true;
                                hitInfo        = _hitInfo;
                                minHitDistance = hitDistance;
                            }
                            else if (hitDistance <= minHitDistance)
                            {
                                hitInfo        = _hitInfo;
                                minHitDistance = hitDistance;
                            }
                        }
                    }
                }


                int hL = -100;
                int hR = 100;

                //查找hL、hR
                if (hasHit)
                {
                    radius = (hitInfo.collider as SphereCollider).radius * hitInfo.collider.transform.lossyScale.x;
                    pos    = hitInfo.collider.transform.position;

                    foreach (Path path in paths)
                    {
                        if (path.hit == null)
                        {
                            continue;
                        }
                        if (path.hit != hitInfo.collider.gameObject)
                        {
                            continue;
                        }

                        int index   = path.index;
                        int myIndex = index;


                        List <Vector3> list   = path.list;
                        List <Vector3> points = path.points;


                        Vector3 p1 = list[i];
                        Vector3 p2 = list[i + 1];

                        Vector3 d0    = p2 - p1;
                        Vector3 d1    = pos - p1;
                        Vector3 cross = Vector3.Cross(d1, d0);

                        if (cross.y < 0)
                        {
                            if (myIndex <= hR)
                            {
                                hR = myIndex;
                            }
                        }
                        else
                        {
                            if (myIndex >= hL)
                            {
                                hL = myIndex;
                            }
                        }
                    }
                }

                if (hasHit)
                {
                    if (hR == 100)
                    {
                        hR = hL + 1;
                    }
                    if (hL == -100)
                    {
                        hL = hR - 1;
                    }

//					Debug.Log("hL=" + hL + "  hR=" + hR);
                    foreach (Path path in paths)
                    {
                        int index   = path.index;
                        int myIndex = index;

                        List <Vector3> list   = path.list;
                        List <Vector3> points = path.points;


                        Vector3 p1 = list[i];
                        Vector3 p2 = list[i + 1];

                        int symbol   = 1;
                        int gapIndex = 0;
                        if (myIndex <= hL)
                        {
                            gapIndex = myIndex - hL - 1;
                            symbol   = -1;
                        }
                        else
                        {
                            gapIndex = myIndex - hR + 1;
                            symbol   = 1;
                        }


                        Vector3 direction = (p2 - p1).normalized;
                        float   radian    = HMath.radian(0, 0, direction.x, direction.z);
                        radian += Mathf.PI * 0.5F;

                        Vector3 ip = HMath.IntersectionPoint(pos, p1, p2);
                        float   r  = radius * symbol + gap * gapIndex;


//						Debug.Log("index="+index + "  gapIndex=" + gapIndex + " r=" + r);


                        Vector3 p;

                        if (Vector3.Distance(ip, p1) > radius * hitRadiusMultiply * hitRadiusMultiplyVertical)
                        {
                            p = ip - direction * radius * hitRadiusMultiply * hitRadiusMultiplyVertical;
                            points.Add(p);
                        }

                        p   = Vector3.zero;
                        p.x = HMath.radianPointX(radian, r, pos.x);
                        p.z = HMath.radianPointY(radian, r, pos.z);
                        points.Add(p);

                        if (Vector3.Distance(ip, p2) > radius * hitRadiusMultiply * hitRadiusMultiplyVertical)
                        {
                            p = ip + direction * radius * hitRadiusMultiply * hitRadiusMultiplyVertical;
                            points.Add(p);
                            list.Insert(i + 1, p);
                        }
                        else
                        {
                            list.Insert(i + 1, p2);
                        }
                    }


                    listCount = paths[0].list.Count;
                }
            }
        }