void TriggerStop(GameObject vehicle)
        {
            //Check if there are other cars in the queue
            //if that's the case, stop the vehicle

            // if(vehiclesQueue.Count > 0){
            //     vehicle.GetComponent<CarAI>().hasToStop = true;
            // }
            // vehiclesQueue.Add(vehicle);

            CarAI carAI = vehicle.GetComponent <CarAI>();

            if (!IsOnPrioritySegment(carAI))
            {
                if (vehiclesQueue.Count > 0 || vehiclesInIntersection.Count > 0)
                {
                    carAI.hasToStop = true;
                    vehiclesQueue.Add(vehicle);
                }
                else
                {
                    vehiclesInIntersection.Add(vehicle);
                    carAI.hasToGo = true;
                }
            }
            else
            {
                vehiclesInIntersection.Add(vehicle);
            }
        }
 bool IsOnPrioritySegment(CarAI carAI)
 {
     foreach (Segment nsSeg in prioritySegments)
     {
         if (carAI.curSeg == nsSeg.id)
         {
             return(true);
         }
     }
     return(false);
 }
Example #3
0
        /// If vehicle within raycast hit length, return its speed. Otherwise return the original initial top speed of the vehicle.
        /// In order to avoid that the current car is going fast than the front one and collide.
        /// If the car has to stop, return 0
        float GetCarSpeed()
        {
            //If car has to stop, set speed to 0
            if (hasToStop)
            {
                return(0);
            }

            Vector3 anchor = new Vector3(this.transform.position.x, this.transform.position.y + 1f, this.transform.position.z);

            if (raycastAnchor != null)
            {
                anchor = raycastAnchor.position;
            }

            //Check if we are going to collide with a car in front
            CarAI otherCarAI = null;
            float topSpeed   = initialTopSpeed;
            float initRay    = (raysNumber / 2f) * raySpacing;

            for (float a = -initRay; a <= initRay; a += raySpacing)
            {
                float hitDist;
                CastRay(anchor, a, this.transform.forward, raycastLength, out otherCarAI, out hitDist);

                //If rays collide with a car, adapt the top speed to be the same as the one of the front vehicle
                if (otherCarAI != null && otherCarAI.carController != null && carController.Topspeed > otherCarAI.carController.Topspeed)
                {
                    //Check if the car is on the same lane or not. If not the same lane, then we do not adapt the vehicle speed to the one in front
                    //(it just means that the rays are hitting a car on the opposite lane...which shouldn't influence the car's speed)
                    if (hasToGo && !IsOnSameLane(otherCarAI.transform))
                    {
                        return(topSpeed);
                    }

                    //If the hit distance is too close, "emergency slow down" the car so they don't collide
                    else if (hitDist < 2f)
                    {
                        return(topSpeed = 0f);
                    }

                    //Otherwise adapt the car speed to the one in front
                    topSpeed = otherCarAI.carController.Topspeed - 0.05f;
                    break;
                }
            }

            //If no collision detected then keep the car top speed
            return(topSpeed);
        }
Example #4
0
        void CastRay(Vector3 anchor, float angle, Vector3 dir, float length, out CarAI outCarAI, out float outHitDistance)
        {
            outCarAI       = null;
            outHitDistance = -1f;

            //Draw raycast
            Debug.DrawRay(anchor, Quaternion.Euler(0, angle, 0) * dir * length, new Color(1, 0, 0, 0.5f));

            //Detect hit only on the autonomous vehicle layer
            int        layer = 1 << LayerMask.NameToLayer("AutonomousVehicle");
            RaycastHit hit;

            if (Physics.Raycast(anchor, Quaternion.Euler(0, angle, 0) * dir, out hit, length, layer))
            {
                outCarAI       = hit.collider.GetComponentInParent <CarAI>();
                outHitDistance = hit.distance;
            }
        }
        /// If vehicle within raycast hit length, return its speed. Otherwise return the original initial top speed of the vehicle.
        /// In order to avoid that the current car is going fast than the front one and collide.
        /// If the car has to stop, return 0
        float GetCarSpeed()
        {
            //If car has to stop, set speed to 0
            if (hasToStop)
            {
                return(0);
            }

            Vector3 anchor = new Vector3(this.transform.position.x, this.transform.position.y + 1f, this.transform.position.z);

            if (raycastAnchor != null)
            {
                anchor = raycastAnchor.position;
            }

            //Check if we are going to collide with a car in front
            CarAI otherCarAI = null;
            float topSpeed   = initialTopSpeed;
            float initRay    = (raysNumber / 2f) * raySpacing;

            for (float a = -initRay; a <= initRay; a += raySpacing)
            {
                otherCarAI = CastRay(anchor, a, this.transform.forward, raycastLength);

                if (otherCarAI != null && otherCarAI.carController != null && carController.Topspeed > otherCarAI.carController.Topspeed)
                {
                    //If the car is in an intersection and the flag for has to go is true, ignore any collision and just make the car
                    //go forward
                    if (hasToGo)
                    {
                        return(topSpeed);
                    }

                    topSpeed = otherCarAI.carController.Topspeed;
                    break;
                }
            }
            return(topSpeed);
        }