IEnumerator LaneKeeping(float waitTime) { while (true) { yield return(new WaitForSeconds(waitTime)); RaycastHit hit; CurvySpline curvySpline = null; CurvySpline pathEnhancedCurvySpline = null; if (Physics.Raycast(rayCastPos.position, rayCastPos.TransformDirection(-Vector3.up), out hit, 10.0f, 1 << LayerMask.NameToLayer("Graphics"))) { curvySpline = hit.collider.gameObject.GetComponent <CurvySpline>(); pathEnhancedCurvySpline = pathEnhanced.GetComponent <CurvySpline>(); if (curvySpline.IsInitialized && pathEnhancedCurvySpline.IsInitialized) { //Debug.Log("meshcollider found is: " + hit.collider); float carTf = curvySpline.GetNearestPointTF(transform.position); //determine the nearest t of the car with respect to the centerLine Vector3 centerLinePoint = curvySpline.Interpolate(carTf); float dist = Vector3.Distance(transform.position, centerLinePoint); Lane lane = MonitorLane(centerLinePoint); //this is to understand if I am partially in the oncoming lane if (dist < 2.0f && lane == Lane.RIGHT) //if dist2 < 2.7f I am in the right lane { linesUtilsAlt.CenterLineColor = linesUtilsAlt.ChangeMatByDistance(dist / 2.0f); SetDashBoardColor(dist, 2.0f); } else if (dist >= 2.0f && lane == Lane.RIGHT) { linesUtilsAlt.CenterLineColor = new Color32(0x00, 0xFF, 0x00, 0x00); laneState = LaneState.GREEN; } else if (lane == Lane.OPPOSITE) // this is to draw the redLine only when the PlayerCar is moving to the left, if it is moving to the right the line doesn't have to be red! { linesUtilsAlt.CenterLineColor = new Color32(0xFF, 0x00, 0x00, 0xFF); laneState = LaneState.RED; } linesUtilsAlt.DrawCenterLine(gameObject, carTf, curvySpline, ResourceHandler.instance.sprites[36].texture); } } else { laneState = LaneState.GREEN; } } }
void SetDashBoardColor(float dist, float normFactor) { float value = Mathf.Clamp(dist / normFactor, 0.0f, 1.0f); if (value <= 0.4f) { laneState = LaneState.RED; } else if (value > 0.4f && value <= 0.8f) { laneState = LaneState.YELLOW; } else { laneState = LaneState.GREEN; } }
IEnumerator LaneKeeping(float waitTime) { while (true) { yield return(new WaitForSeconds(waitTime)); RaycastHit hit; CurvySpline curvySpline = null; CurvySpline pathEnhancedCurvySpline = null; if (Physics.Raycast(rayCastPos.position, rayCastPos.TransformDirection(-Vector3.up), out hit, 10.0f, 1 << LayerMask.NameToLayer("Graphics"))) { curvySpline = hit.collider.gameObject.GetComponent <CurvySpline>(); pathEnhancedCurvySpline = pathEnhanced.GetComponent <CurvySpline>(); if (curvySpline.IsInitialized && pathEnhancedCurvySpline.IsInitialized) { float carTf = curvySpline.GetNearestPointTF(transform.position); //determine the nearest t of the car with respect to the centerLine float carTf2 = pathEnhancedCurvySpline.GetNearestPointTF(transform.position); float dist = Vector3.Distance(transform.position, curvySpline.Interpolate(carTf)); Lane lane = MonitorLane(pathEnhancedCurvySpline.GetTangentFast(carTf2)); //this is to understand if I am partially in the oncoming lane if (dist < 4.0f && lane == Lane.RIGHT) { linesUtilsAlt.CenterLineColor = linesUtilsAlt.ChangeMatByDistance(dist / 4.0f); SetDashBoardColor(dist, 4.0f); } else if (dist >= 4.0f && lane == Lane.RIGHT) { linesUtilsAlt.CenterLineColor = new Color32(0x00, 0xFF, 0x00, 0x00); laneState = LaneState.GREEN; } else if (lane == Lane.OPPOSITE) { linesUtilsAlt.CenterLineColor = new Color32(0xFF, 0x00, 0x00, 0xFF); laneState = LaneState.RED; } linesUtilsAlt.DrawCenterLine(carTf, MonitorLane(curvySpline.GetTangentFast(carTf)), curvySpline, ResourceHandler.instance.sprites[33].texture); } } else { laneState = LaneState.GREEN; } } }