Example #1
0
    public void GetIntersection()
    {
        var allIntersections = FindObjectsOfType <IntersectionComponent>();

        foreach (var item in allIntersections)
        {
            if (!item.gameObject.activeInHierarchy)   //skip inactive ones
            {
                continue;
            }

            if (Vector3.Distance(this.transform.position, item.transform.position) < intersectionRange)
            {
                intersectionC = item;
                break;
            }
        }

        SetIntersectionLaneData();

        if (intersectionC == null)
        {
            Debug.LogError("Error finding intersection, check range");
        }
    }
 private void ResetData()
 {
     StopAllCoroutines();
     currentMapLaneSegmentBuilder = null;
     currentIntersectionComponent = null;
     if (prevMapLaneSegmentBuilder?.stopLine?.mapIntersectionBuilder != null)
     {
         prevMapLaneSegmentBuilder.stopLine.mapIntersectionBuilder.ExitStopSignQueue(this);
     }
     prevMapLaneSegmentBuilder = null;
     currentNPCLightState      = NPCLightStateTypes.Off;
     allRenderers.ForEach(x => SetNPCLightRenderers(x));
     currentSpeed          = 0f;
     currentStopTime       = 0f;
     path                  = 0f;
     currentSpeed_measured = 0f;
     rb.angularVelocity    = Vector3.zero;
     rb.velocity           = Vector3.zero;
     isCurve               = false;
     isLeftTurn            = false;
     isRightTurn           = false;
     isStopLight           = false;
     isStopSign            = false;
     hasReachedStopSign    = false;
     isLaneDataSet         = false;
 }
    public void GetIntersection()
    {
        mapIntersectionLanes.AddRange(transform.GetComponentsInChildren <MapLaneSegmentBuilder>());

        var allIntersections = FindObjectsOfType <IntersectionComponent>();

        foreach (var item in allIntersections)
        {
            if (!item.gameObject.activeInHierarchy)   //skip inactive ones
            {
                continue;
            }

            if (Vector3.Distance(this.transform.position, item.transform.position) < intersectionRange)
            {
                intersectionC = item;
                break;
            }
        }

        if (intersectionC == null)
        {
            Debug.LogError("Error finding intersection, check range");
        }
    }
    private void EvaluateTarget()
    {
        distanceToCurrentTarget = Vector3.Distance(new Vector3(frontCenter.position.x, 0f, frontCenter.position.z), new Vector3(currentTarget.x, 0f, currentTarget.z));
        distanceToStopTarget    = Vector3.Distance(new Vector3(frontCenter.position.x, 0f, frontCenter.position.z), new Vector3(stopTarget.x, 0f, stopTarget.z));

        if (Vector3.Dot(frontCenter.forward, (currentTarget - frontCenter.position).normalized) < 0 || distanceToCurrentTarget < 1f)
        {
            if (currentIndex == laneData.Count - 2)                 // reached 2nd to last target index see if stop line is present
            {
                if (currentMapLaneSegmentBuilder?.stopLine != null) // check if stopline is connected to current path
                {
                    currentIntersectionComponent = currentMapLaneSegmentBuilder.stopLine?.mapIntersectionBuilder?.intersectionC;
                    stopTarget = currentMapLaneSegmentBuilder.segment.targetWorldPositions[currentMapLaneSegmentBuilder.segment.targetWorldPositions.Count - 1];
                    prevMapLaneSegmentBuilder = currentMapLaneSegmentBuilder;
                    if (prevMapLaneSegmentBuilder.stopLine.mapIntersectionBuilder != null)        // null if map not setup right TODO add check to report missing stopline
                    {
                        if (prevMapLaneSegmentBuilder.stopLine.mapIntersectionBuilder.isStopSign) // stop sign
                        {
                            StartCoroutine(WaitStopSign());
                        }
                        else
                        {
                            StartCoroutine(WaitTrafficLight());
                        }
                    }
                    GetTurnSignal();
                }
            }

            if (currentIndex < laneData.Count - 2)
            {
                currentIndex++;
                currentTarget  = laneData[currentIndex];
                CurrentPath[0] = CurrentPath[1];
                CurrentPath[1] = CurrentPath[2];
                CurrentPath[2] = laneData[currentIndex + 1];
            }
            else if (currentIndex < laneData.Count - 1) // reached target dist and is not at last index of lane data
            {
                currentIndex++;
                currentTarget  = laneData[currentIndex];
                CurrentPath[0] = CurrentPath[1];
                CurrentPath[1] = CurrentPath[2];
                CurrentPath[2] = CurrentPath[2];
            }
            else
            {
                GetNextLane();
            }
        }
    }