示例#1
0
 // Start is called before the first frame update
 void Start()
 {
     shield        = gameObject.GetComponentInChildren <SphereCollider>();
     material      = shield.GetComponentInParent <Renderer>().material;
     playerControl = gameObject.GetComponent <PlayerControl>();
 }
示例#2
0
        /// <summary>
        /// Look for the closest Street End or Start Collider
        /// </summary>
        /// <param name="_hitPoint">The Pos from where to look</param>
        /// <returns>The GameObject closest from the Pos (null if its too far away)</returns>
        private Connection FindClosestConnection(Vector3 _hitPoint)
        {
            Collider[]            colls         = Physics.OverlapSphere(_hitPoint, 2); //Cast a Sphere on the give Pos
            List <SphereCollider> validCollider = new List <SphereCollider>();
            List <SphereCollider> sphereColls   = new List <SphereCollider>();

            for (int i = 0; i < colls.Length; i++)
            {
                if (colls[i] is SphereCollider)
                {
                    sphereColls.Add((SphereCollider)colls[i]);
                }
            }

            Street tmpStreet;
            Cross  tmpCross;

            //look for all collider if there are valid for connection
            for (int i = 0; i < sphereColls.Count; i++)
            {
                tmpStreet = sphereColls[i].GetComponentInParent <Street>();
                if (tmpStreet != null && ((sphereColls[i].CompareTag("StreetEnd") && tmpStreet.m_EndIsConnectable) ||
                                          (sphereColls[i].CompareTag("StreetStart") && tmpStreet.m_StartIsConnectable)))
                {
                    validCollider.Add(sphereColls[i]); //if found a valid GameObject add it to a List
                }
                tmpCross = sphereColls[i].GetComponent <Cross>();
                if (tmpCross != null && tmpCross.IsConnectabel(sphereColls[i]))
                {
                    validCollider.Add(sphereColls[i]);
                }
            }

            if (validCollider.Count == 0)
            {
                return(null);                                                                                                     //return null if no valid Collider was found in the Range
            }
            SphereCollider closestCollider = GetClosesetCollider(validCollider, _hitPoint);                                       //Look for the closest GameObject of all valid GameObjects

            Cursor.SetPosition(closestCollider.transform.position + closestCollider.transform.rotation * closestCollider.center); //Set the Sphere to the Pos of the closest valid Street GameObject
            tmpStreet = closestCollider.GetComponentInParent <Street>();
            tmpCross  = closestCollider.GetComponentInParent <Cross>();
            Connection conn = null;

            if (tmpStreet != null)
            {
                if (closestCollider.CompareTag("StreetStart"))
                {
                    conn = tmpStreet.GetStartConnection();
                }
                else if (closestCollider.CompareTag("StreetEnd"))
                {
                    conn = tmpStreet.m_EndConnection;
                }
            }
            else if (tmpCross != null)
            {
                Vector3 collCenter = closestCollider.center;
                if (collCenter.x == 0 && collCenter.z == -1.2f)
                {
                    conn = tmpCross.GetConnectionByIndex(0);
                }
                else
                if (collCenter.x == -1.2f && collCenter.z == 0)
                {
                    conn = tmpCross.GetConnectionByIndex(1);
                }
                else
                if (collCenter.x == 0 && collCenter.z == 1.2f)
                {
                    conn = tmpCross.GetConnectionByIndex(2);
                }
                else
                if (collCenter.x == 1.2f && collCenter.z == 0)
                {
                    conn = tmpCross.GetConnectionByIndex(3);
                }
            }
            return(conn);
        }