private void OnTriggerEnter(Collider other)
        {
            if (_isStopped && !_canCollide && other == this)
            {
                return;
            }

            if (other.transform.tag == "TrafficLight")
            {
                Debug.Log(transform.name + " Colliding with Traffic Light");

                // Check is this is the frontmost car
                if (_frontCar != null && !_isFront)
                {
                    Debug.Log(transform.name + " _frontCar is not null or _isFront is false - OnTrigger TrafficLight************************");
                    return;
                }

                _isFront  = true;
                _frontCar = this;

                // Add car to the traffic list
                _trafficCont = other.GetComponent <TrafficLightScript>();
                _trafficCont.AddToTrafficList(this);

                StopCar();
            }

            if (other.transform.tag == "Vehicle")
            {
                Debug.Log(transform.name + " Colliding with " + other.transform.name);

                StopCar();

                // Get the collided car's script
                CarScript otherCar = other.GetComponent <CarScript>();

                // Get reference to the front car
                _frontCar = otherCar._frontCar;

                if (_frontCar == null)
                {
                    Debug.Log(transform.name + " : _frontCar of " + other.transform.name + "is null - OnTrigger Vehicle**************************");
                    return;
                }

                // Add car to the traffic list
                _trafficCont = otherCar._trafficCont;
                _trafficCont.AddToTrafficList(this);
            }
        }
        IEnumerator CanCollideCooldown()
        {
            yield return(new WaitForSeconds(3.0f));

            _canCollide = true;

            //Reset all members
            Destroy(_temp);
            _frontCar    = null;
            _trafficCont = null;

            _isStopped = false;
            _isFront   = false;
        }
Esempio n. 3
0
 private void checkSurroundings()
 {
     RaycastHit2D[] hitObjs = Physics2D.RaycastAll(transform.position + .55f * currDirection, currDirection, sensorDistForward, (1 << carLayer | 1 << envLayer));
     if (hitObjs.Length == 0)
     {
         carState = 1;
     }
     else
     {
         foreach (RaycastHit2D rayHit in hitObjs)
         {
             if (rayHit.transform.CompareTag("Intersection"))
             {
                 TrafficLightScript trafLight  = rayHit.transform.GetComponent <TrafficLightScript>();
                 Vector2            lightState = trafLight.queryState();
                 if ((int)lightState.x == 0)
                 {
                     if (rb.velocity.magnitude != 0)
                     {
                         carState = -1;
                     }
                 }
                 else if ((int)lightState.x == 2)
                 {
                     carState = 1;
                 }
                 else
                 {
                     yellowDecision(lightState.y);
                 }
             }
             if (rayHit.transform.CompareTag("Car"))
             {
                 //carState = -1;
                 if (rb.velocity.magnitude != 0)
                 {
                     carState = -1;
                 }
                 //changeLanes();
             }
         }
     }
 }
Esempio n. 4
0
 private void Awake()
 {
     mainScript = GetComponentInParent <TrafficLightScript>();
 }
Esempio n. 5
0
    private void Start()
    {
        lightScript = GetComponent <TrafficLightScript>();

        lightsGO = lightScript.tLights;
    }