예제 #1
0
    /// <summary>
    /// Methode zum Erstellen einer CustomEasyRoad.
    /// </summary>
    /// <param name="car">Das Gameobject des Autos.</param>
    /// <param name="road">Die Straße.</param>
    /// <param name="minCars">Die Mindestanzahl von Autos auf dem Streckenpart.</param>
    /// <param name="maxCars">Die Maximalanzahl von Autos auf dem Streckenpart.</param>
    /// <param name="numberOfTracks">Die Anzahl der Spuren.</param>
    public CustomEasyRoad(GameObject car, ERRoad road, int minCars, int maxCars, int numberOfTracks)
    {
        road.SetTag("Street");

        this.Road        = road;
        this.CarsOnLanes = new List <Tuple <GameObject, int> >();
        this.CarsPerLane = new Dictionary <int, List <GameObject> >();
        for (int i = 0; i < numberOfTracks; i++)
        {
            List <GameObject> carsOnLane = new List <GameObject>();
            CarsPerLane.Add(i, carsOnLane);
        }

        Vector3[] markers  = road.GetSplinePointsCenter();
        Vector3[] markersR = road.GetSplinePointsRightSide();
        Vector3[] markersL = road.GetSplinePointsLeftSide();

        int carCount = Random.Range(minCars, maxCars);

        if (carCount > 0)
        {
            int        increment = markers.Length / carCount;
            Vector3    look      = Vector3.zero;
            GameObject newCar    = null;

            for (int i = 0; i < markers.Length; i += increment)
            {
                // Die Spur bestimmen
                int       lane             = Random.Range(0, numberOfTracks);
                Vector3[] directionMarkers = null;

                // Die Richtung des Autos/Lane holen und setzen
                if (lane < (numberOfTracks / 2))
                {
                    directionMarkers = markersL;
                    look             = (markers[Mathf.Max(0, i - 1)] - markers[Mathf.Min(markers.Length - 1, i + 1)]);
                }
                else
                {
                    directionMarkers = markersR;
                    look             = (markers[Mathf.Min(markers.Length - 1, i + 1)] - markers[Mathf.Max(0, i - 1)]);
                }

                // Den RoadSlerp holen
                float roadSlerp = RoadUtils.GetRoadSlerpByLane(numberOfTracks, lane);

                // Das Car mit der Richtung und der Spur spawnen
                newCar = GameObject.Instantiate(car, Vector3.Slerp(markers[i], directionMarkers[i], roadSlerp) + new Vector3(0, 1, 0), Quaternion.LookRotation(look));

                // Das Auto den Listen hinzufügen
                this.AddToIndexOnLane(lane, newCar);
                CarsOnLanes.Add(new Tuple <GameObject, int>(newCar, numberOfTracks - lane - 1));
            }
        }
    }
예제 #2
0
    /// <summary>
    /// Methode zum Platzieren des Autos am Anfang.
    /// </summary>
    private void PlaceCameraCar()
    {
        // Das Auto auf die rechte erste Spur platzieren
        ERRoad  firstRoad            = network.GetRoads().First();
        Vector3 firstSplineRightSide = firstRoad.GetSplinePointsRightSide()[0];
        Vector3 firstMarker          = firstRoad.GetMarkerPosition(0);
        Vector3 rightMiddleLane      = Vector3.zero;

        // Das Position je nach Anzahl der Spuren interpolieren
        switch (this.numberOfTracks)
        {
        case 8:
            rightMiddleLane = Vector3.Slerp(firstMarker, firstSplineRightSide, 0.85f);
            break;

        case 6:
            rightMiddleLane = Vector3.Slerp(firstMarker, firstSplineRightSide, 0.825f);
            break;

        case 4:
            rightMiddleLane = Vector3.Slerp(firstMarker, firstSplineRightSide, 0.75f);
            break;

        case 2:
        default:
            rightMiddleLane = Vector3.Slerp(firstMarker, firstSplineRightSide, 0.5f);
            break;
        }

        // Das Auto an die richtige Positions etzen
        cameraCar.gameObject.transform.position = rightMiddleLane;
        cameraCar.gameObject.transform.rotation = Quaternion.identity;

        // Ist platziert setzen
        isPlaced = true;
    }