/// <summary> /// Methode zum Fahren des Autos (Simulation). /// </summary> public void SimulateCar() { // Die Collider des Autos holen Collider[] colliders = Physics.OverlapBox(cameraCar.gameObject.transform.position, (cameraCar.gameObject.transform.localScale / 5f), cameraCar.gameObject.transform.rotation); List <Collider> carColliders = new List <Collider>(); ERRoad road = null; foreach (Collider collider in colliders) { if (collider.tag == "Street") { road = network.GetRoadByName(collider.name); } if (collider.tag == "Car") { carColliders.Add(collider); } } Vector3 heading = new Vector3(0, 0, 1); if (road != null) { // Hole den letzten Punkt der Strecke Vector3[] markers = road.GetMarkerPositions(); Vector3 lastMarker = markers.Last(); // Die richtige Rotation ausrechnen heading = (lastMarker - markers[markers.Length - 2]).normalized; heading.y = 0; CustomEasyRoad customEasy = null; foreach (CustomEasyRoad customEasyRoad in customEasyRoads) { if (customEasyRoad.Road.GetName() == road.GetName()) { customEasy = customEasyRoad; break; } } //Tuple<Vector3, Vector3> markers = customEasy.GetIncludingMarkers(cameraCar.gameObject.transform.position); //heading = (markers.Second - markers.First).normalized; //heading.y = 0; } // Geschwindigkeit setzen Rigidbody rigidbody = cameraCar.GetComponent <Rigidbody>(); cameraCar.transform.Translate(Vector3.forward * (carSpeed / 3.6f) * Time.deltaTime); //cameraCar.transform.rotation.SetLookRotation(heading); cameraCar.transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(heading), 2.5f * Time.deltaTime); // Die Autos im Weg entfernen foreach (Collider collider in carColliders) { Destroy(collider.gameObject); } }
private void PlaceBuildings(Vector3[] markersSide, Vector3[] markersCenter, bool rightSide, ERRoad road) { try { //Process: //Get a building from the list, //Measure the building's width //Get the current sidePoint's position and count points until the difference is greater than/equal to the width of the building. If the end of the side markers is reached, don't place building //Get the angle based on the two points //Place the building at the center point of those two points //Move the building back away from edge of sidewalk (Min/Max values in the editors) //Optional: Move buildings back from road //Optional: Allow space between buildings float offset = 0; int currentMarker = 0; bool isProcessing = true; bool isFound = false; //Check if the building fits on this street-block float buildingDistance; const float increment = 0.2f; //The value incremented back from the road. do { //If there are no buildings left, no need to prcess anything, obviously. if (buildings.Count == 0) { isProcessing = false; break; } isFound = false; //reset Vector3 startMarker = markersSide[currentMarker]; GameObject building = buildings[buildings.Count - 1]; //Get the last one, and build in reverse. (allows removal from List without any issues) int buildingDepth = (building.GetComponent <BuildingModifier>().buildingDepth *buildingSizeMultiplier); int buildingWidth = (building.GetComponent <BuildingModifier>().buildingWidth *buildingSizeMultiplier); //loop to find the end position for (int i = (currentMarker + 1); i < markersSide.Length; i++) { Vector3 endMarker = markersSide[i]; buildingDistance = Vector3.Distance(startMarker, endMarker); if (buildingDistance >= buildingDepth) { Vector3[] vectors = new Vector3[] { startMarker, endMarker }; Vector3 centerPoint = CenterOfVectors(vectors); bool isRoad = false; offset = 0; //Reset do { isRoad = isInRoad(road.GetName(), centerPoint); //If this is on the road, then we need to move back a little bit until it's not touching the road if (isRoad) { offset += increment; var x = (currentMarker + i) / 2; centerPoint = markersCenter[x] + (markersCenter[x] - centerPoint) * offset; } } while (isRoad); //Optional: Push the building back farther from the road if (minBuildingBack > 0 || maxBuildingBack > 0) { var percent = Random.Range(1, 100); //This option allow a minimal amount of buildings to be moved back if (percent <= percentBuildingBack) { var offsetAddition = Random.Range(minBuildingBack, maxBuildingBack); if (offset == 0) { offsetAddition += (road.GetWidth() * .1f); } offset += offsetAddition; } } //if there is an offset value, push the building back farther if (offset > 0) { startMarker = markersCenter[currentMarker] + (markersCenter[currentMarker] - markersSide[currentMarker]) * -(offset / 2); endMarker = markersCenter[i] + (markersCenter[i] - markersSide[i]) * -(offset / 2); } //Need to move back the start and end markers if (rightSide) { building.transform.position = startMarker; building.transform.rotation = Quaternion.LookRotation(endMarker - startMarker, Vector3.up); } else { building.transform.position = endMarker; building.transform.rotation = Quaternion.LookRotation(startMarker - endMarker, Vector3.up); } isFound = true; currentMarker = i; break; } } if (isFound) { buildings.Remove(building); //Only remove this from the list if the building was placed } else { isProcessing = false; } //Optional: Allow space between buildings if (this.minBetweenBuildings > 0 && this.maxBetweenBuildings > 0) { isFound = false; //Reset float distanceBetweenBuildings = Random.Range(minBetweenBuildings, maxBetweenBuildings); Vector3 startPoint = markersSide[currentMarker]; for (int i = (currentMarker + 1); i < markersSide.Length; i++) { Vector3 endPoint = markersSide[i]; var buildingSeparation = Vector3.Distance(startPoint, endPoint); if (buildingSeparation >= distanceBetweenBuildings) { currentMarker = i; isFound = true; break; } } //In case we ran out of room for this side of the road, stop processing isProcessing = isFound; } } while (isProcessing); } catch (System.Exception ex) { Debug.LogError("Place Buildings Ex: " + ex.Message); } }