예제 #1
0
        /// <summary>
        /// Places a car on a roadobject
        /// </summary>
        /// <param name="currentRoadObject"></param>
        public void PlaceCar(RoadObject currentRoadObject)
        {
            GameObject tempCarObject = Instantiate(carPrefab, new Vector3(0f, 0f, 0f), Quaternion.identity);
            Car        tempCar       = tempCarObject.GetComponent <Car>();

            tempCar.currentRoad = currentRoadObject;
            tempCarObject.transform.SetParent(this.transform);
            allCars.Add(tempCar);
        }
예제 #2
0
 /// <summary>
 /// Generate the road based on ID
 /// </summary>
 /// <param name="road"></param>
 public void GenerateRoad(RoadItem road)
 {
     if (road.properties.highway != "cycleway" && road.properties.highway != "pedestrian" && road.properties.highway != "service")
     {
         GameObject temp = Instantiate(roadObject, new Vector3(0f, 0f, 0f), Quaternion.identity);
         temp.transform.SetParent(this.transform);
         temp.name = road.properties.name;
         RoadObject tempRoadObject = temp.GetComponent <RoadObject>();
         tempRoadObject.Intiate(road);
         allLoadedRoads.Add(tempRoadObject);
     }
 }
예제 #3
0
        /// <summary>
        /// Moves the car and looks for new roads if the car needs a new road
        /// </summary>
        /// <param name="compensationVector"></param>
        public void MoveCar(Vector3 compensationVector)
        {
            if (currentRoad.roadPoints.Count > currentRoadIndex)
            {
                // calculates the look height of the car based on the road and the cars point
                Vector3 tempLook = new Vector3(currentRoad.roadPoints[currentRoadIndex].pointCoordinates.x, compensationVector.y, currentRoad.roadPoints[currentRoadIndex].pointCoordinates.z);
                if (Vector3.Distance(transform.position, tempLook) > 1f)
                {
                    lastRecordedHeight = compensationVector.y;
                    // puts the car on the road
                    transform.position = compensationVector;
                    // looks at the point where the car is driving
                    transform.LookAt(tempLook); // MAYBE U CAN PUT THIS IN THE ELSE SO ITS ONLY EXECUTED ONCE????
                                                // propels the car forward
                    gameObject.transform.Translate(transform.forward * Time.deltaTime * speed * vehicleFrameSpeedCompensator, Space.World);
                }
                else
                {
                    // goes to the next point on the road list
                    currentRoadIndex++;
                }

                // car stuck timer, as long as the car is moving it will add time to it
                carResetTimeStamp = Time.time + maxWaitingTime;
            }
            else
            {
                // optimization by executing once every 10 frames
                findRoadLoopFrames++;
                if (findRoadLoopFrames % 10 == 0 && nextRoad == null)
                {
                    transform.position = compensationVector;
                    // resets the point DEMO ONLY
                    foreach (RoadObject obj in GenerateRoads.Instance.shuffledRoadsList)
                    {
                        // calculates distance between the car and the 1st object of the found road, this should indicate wether the road is close or not
                        float distance = Vector3.Distance(transform.position, obj.roadPoints[0].pointCoordinates);

                        // finds new road segment based on distance
                        if (distance < Mathf.Round(Random.Range(7, 13)) && currentRoad != obj && obj != lastRoad)
                        {
                            if (Vector3.Distance(transform.position, obj.roadPoints[0].pointCoordinates) < Vector3.Distance(transform.position, obj.roadPoints[obj.roadPoints.Count - 1].pointCoordinates))
                            {
                                // checks if the point is infront of the player or behind so the car doesn't do "illegal" weird turns
                                Vector3 toTarget = (obj.roadPoints[0].pointCoordinates - transform.position).normalized;
                                if (Vector3.Dot(toTarget, transform.forward) > 0)
                                {
                                    // assigns the newly found road
                                    lastRoad    = currentRoad;
                                    currentRoad = obj;
                                    nextRoad    = obj;
                                    break;
                                }
                            }
                        }
                        else if (Time.time > carResetTimeStamp)
                        {
                            // if the car is stuck, it will reset the position to a random road on the map
                            lastRoad           = currentRoad;
                            currentRoad        = obj;
                            transform.position = obj.roadPoints[0].pointCoordinates;
                            currentRoadIndex   = 0;
                            nextRoad           = null;
                            break;
                        }
                    }
                }
                if (nextRoad != null)
                {
                    // moves the car to the next roads starting point
                    Vector3 tempLook            = new Vector3(currentRoad.roadPoints[0].pointCoordinates.x, compensationVector.y, currentRoad.roadPoints[0].pointCoordinates.z);
                    float   distanceToRoadPoint = Vector3.Distance(transform.position, tempLook);
                    if (distanceToRoadPoint > 1f)
                    {
                        transform.position = compensationVector;
                        // looks at the point where the car is driving
                        transform.LookAt(tempLook);
                        // propels the car forward
                        gameObject.transform.Translate(transform.forward * Time.deltaTime * speed * vehicleFrameSpeedCompensator, Space.World);
                    }
                    else
                    {
                        currentRoadIndex = 0;
                        nextRoad         = null;
                    }
                }
            }
        }