示例#1
0
    private void Start()
    {
        if (creation.Count == 0)
        {
            return;
        }

        trackPath          = GetComponent <TrackPath>();
        pointsAndPrefabs   = new Dictionary <GameObject, GameobjectsToCreate>();
        pointsAndCreations = new Dictionary <GameObject, GameObject>();
        listOfPoints       = new List <GameObject>();


        //определяем количество создаваемых объектов
        int sumPointToCreate = 0;

        foreach (GameobjectsToCreate gameobjectsToCreate in creation)
        {
            sumPointToCreate += gameobjectsToCreate.count;
        }

        //Длина интервала между точками спавна объектов
        float entitieDistance = trackPath.GetTrackDistance() / (sumPointToCreate + 1);
        float currentDistance = 0.5f;

        //создаём точки спавна объектов
        for (int i = 0; i < sumPointToCreate; i++)
        {
            currentDistance += entitieDistance;

            KeyValuePair <Transform, float> cKVP = trackPath.GetChekpointThrouDistance(currentDistance);


            Vector3 curPoint  = cKVP.Key.position;
            Vector3 nextPoint = trackPath.GetNextCheckPointPosition(cKVP.Key).position;
            float   m1        = currentDistance - cKVP.Value;
            float   m2        = Vector3.Distance(curPoint, nextPoint) - m1;

            float x = (m2 * curPoint.x + m1 * nextPoint.x) / (m1 + m2);
            float y = (m2 * curPoint.y + m1 * nextPoint.y) / (m1 + m2);
            float z = (m2 * curPoint.z + m1 * nextPoint.z) / (m1 + m2);



            Vector3 posToCreate = new Vector3(x, y + 100f, z);

            GameObject pointOfCreation = Instantiate(pointCreator, posToCreate, Quaternion.identity, gameObject.transform);

            Vector3 posToLookAt = nextPoint;
            posToLookAt.y = pointOfCreation.transform.position.y;

            pointOfCreation.transform.LookAt(posToLookAt);

            pointsAndCreations.Add(pointOfCreation, default);
            listOfPoints.Add(pointOfCreation);
        }

        ///создаём Список всех создаваемых префабов по их количеству
        List <GameobjectsToCreate> creaturesCountList = new List <GameobjectsToCreate>();

        foreach (var item in creation)
        {
            for (int i = 0; i < item.count; i++)
            {
                creaturesCountList.Add(item);
            }
        }

        //раздаём точкам тип префаба
        foreach (GameObject pointOfSpawn in listOfPoints)
        {
            int curRnd = UnityEngine.Random.Range(0, creaturesCountList.Count);

            pointsAndPrefabs.Add(pointOfSpawn, creaturesCountList[curRnd]);
            creaturesCountList.RemoveAt(curRnd);
        }

        CheckAndCreateEntities();

        if (generateConstantly)
        {
            StartCoroutine(CreateEntitiesCorut());
        }
    }