//Returns true if not waiting for red light and not in front of another vehicle (or if this vehicle has movement priority over other vehicle) private bool PathIsClear() { isInFrontOfRedLight = TrafficLightIsRed(); if (trafficLightBarrier != null && !isInFrontOfRedLight) { //No longer waiting for red light trafficLightBarrier = null; } //Other vehicle disappeared if (otherVehicle = null) { isBehindOtherVehicle = false; } if (isInFrontOfOpenBridge) { if (waitingForBridge.state == DeckState.Closed) { isInFrontOfOpenBridge = false; } } return(!isInFrontOfRedLight && !isInFrontOfOpenBridge && (!isBehindOtherVehicle || hasPriority)); }
// Start is called before the first frame update void Start() { //Finds the barrier which is a child of this trafficlight object barrier = GetComponentInChildren <Trafficlight_Barrier>(); timeTillChange = Random.Range(4f, 18f); }
// Start is called before the first frame update void Start() { barrier = GetComponentInChildren <Trafficlight_Barrier>(); if (barrier == null) { Debug.LogError("ERROR: No trafficlight_barrier found in warninglight object"); } timeTillChange = Random.Range(4f, 18f); //Only used when not listening to controller }
//Fires when colliding with another collider private void OnTriggerEnter(Collider other) { //Calculate the dotproduct between transform and target transform. (used for finding out how the target unit is facing us) float dotForward = Vector3.Dot(other.transform.forward, transform.forward); //Traffic light in-sight if (other.transform.tag == "Trafficlight_Barrier") { //If train and barrier is facing the other way, ignore it... if (VehicleType == TrafficType.Train && dotForward > -0.8f) { return; } //Barrier is facing this unit the opposite way & unit is crossing bridge or track, ignore opposing barrier.. else if (isCrossingBridgeOrTrack && dotForward > 0.8f) { return; } //Pedestrian or Cyclist has just crossed the road and is facing a red light barrier on the opposite side. Ignore it.. else if ((VehicleType == TrafficType.Bicycle || VehicleType == TrafficType.Pedestrian) && other.GetComponentInParent <Trafficlight>() != null && dotForward > 0.8f) { return; } //Barrier is facing this unit at an angle. The barrier is probably not meant for this unit, ignore it.. else if (dotForward > -0.8f && dotForward < 0.8f) { return; } Trafficlight_Barrier barrier = other.GetComponent <Trafficlight_Barrier>(); if (barrier.IsActive) //(trafficlight_barrier is only active when trafficlight is red) { isInFrontOfRedLight = true; trafficLightBarrier = barrier; } } //Stop when bridge is closed if (other.transform.tag == "Bridge" && VehicleType != TrafficType.Boat) { Bridge bridge = other.GetComponent <Bridge>(); isCrossingBridgeOrTrack = true; if (bridge.state == DeckState.Open) { isInFrontOfOpenBridge = true; waitingForBridge = bridge; } } if (other.transform.tag == "Track" && VehicleType != TrafficType.Train) { isCrossingBridgeOrTrack = true; } //In front of other (waiting) vehicle if (CollidesWithTags.Contains(other.transform.tag)) { //Other unit is facing this unit almost head on. Move through target unit. //(Implementing rerouting for cyclists/pedestrians when facing target unit is too complicated) if (VehicleType == TrafficType.Bicycle || VehicleType == TrafficType.Pedestrian) { if (dotForward < -0.6f) { return; } } isBehindOtherVehicle = true; otherVehicle = other.GetComponentInParent <WaypointMovementController>(); //If the other vehicle also collides with this vehicle, give this one priority to move first if (otherVehicle.otherVehicle == this) { if (!otherVehicle.hasPriority) { hasPriority = true; } } } }
void Start() { barrier = gameObject.GetComponent <Trafficlight_Barrier>(); }