Example #1
0
        public override void handle(CarAIControl controller, Dictionary <int, VRAVEObstacle> obstacles,
                                    float currentSpeed,
                                    CarAIControl.BrakeCondition brakeCondition)
        {
            if (!Enable)
            {
                return;
            }

            foreach (VRAVEObstacle vo in obstacles.Values)
            {
                if (vo.obstacleTag.Equals(VRAVEStrings.Crazy_AI_Car))
                {
                    controller.Driving = false;
                    return;
                }
            }
        }
Example #2
0
        // check sensors 8 - 17, ignore otherwise
        // get the distance to the trash can and angle to the trash can
        // if angle is +, steer left
        // if angle is -, steer right ?
        // compute steer amount as fn of distance, angle, and base steer amount (10 degrees?)
        public override void handle(CarAIControl controller, Dictionary <int, VRAVEObstacle> obstacles,
                                    float currentSpeed,
                                    CarAIControl.BrakeCondition brakeCondition)
        {
            if (!Enable)
            {
                return;
            }
            controller.IsAvoidingObstacle = false;
            VRAVEObstacle obs;

            for (int i = scanningRange_Left; i <= scanningRange_Right; ++i)
            {
                if (obstacles.TryGetValue(i, out obs))
                {
                    if (obs.obstacleTag.Equals(VRAVEStrings.Obstacle))
                    {
                        // found trash can
                        controller.IsAvoidingObstacle = true;

                        //float distance = obs.Distance;

                        // calculate the local-relative position of the target, to steer away from
                        Vector3 localTarget = transform.InverseTransformPoint(obs.obstacle.point);

                        // work out the local angle towards the target
                        float targetAngle = Mathf.Atan2(localTarget.x, localTarget.z) * Mathf.Rad2Deg;

                        if (targetAngle <= 0)
                        {
                            targetAngle = -90 + targetAngle;
                        }
                        else
                        {
                            targetAngle = 90 - targetAngle;
                        }

                        controller.ObstacleAvoidanceSteerAmount = -targetAngle * 2f;

                        return;
                    }
                }
            }
        }
Example #3
0
 private void FixedUpdate()
 {
     if (this.target == null || !this.driving)
     {
         float accelBrakeInput = Mathf.Clamp(-this.carController.CurrentSpeed, -1f, 1f);
         this.carController.Move(0f, accelBrakeInput);
     }
     else
     {
         Vector3 to = base.transform.forward;
         if (base.GetComponent <Rigidbody>().velocity.magnitude > this.carController.MaxSpeed * 0.1f)
         {
             to = base.GetComponent <Rigidbody>().velocity;
         }
         float num = this.carController.MaxSpeed;
         CarAIControl.BrakeCondition brakeCondition = this.brakeCondition;
         if (brakeCondition != CarAIControl.BrakeCondition.TargetDirectionDifference)
         {
             if (brakeCondition != CarAIControl.BrakeCondition.TargetDistance)
             {
                 if (brakeCondition != CarAIControl.BrakeCondition.NeverBrake)
                 {
                 }
             }
             else
             {
                 Vector3 vector = this.target.position - base.transform.position;
                 float   b      = Mathf.InverseLerp(this.cautiousMaxDistance, 0f, vector.magnitude);
                 float   value  = base.GetComponent <Rigidbody>().angularVelocity.magnitude *this.cautiousAngularVelocityFactor;
                 float   t      = Mathf.Max(Mathf.InverseLerp(0f, this.cautiousMaxAngle, value), b);
                 num = Mathf.Lerp(this.carController.MaxSpeed, this.carController.MaxSpeed * this.cautiousSpeedFactor, t);
             }
         }
         else
         {
             float b2 = Vector3.Angle(this.target.forward, to);
             float a  = base.GetComponent <Rigidbody>().angularVelocity.magnitude *this.cautiousAngularVelocityFactor;
             float t2 = Mathf.InverseLerp(0f, this.cautiousMaxAngle, Mathf.Max(a, b2));
             num = Mathf.Lerp(this.carController.MaxSpeed, this.carController.MaxSpeed * this.cautiousSpeedFactor, t2);
         }
         Vector3 vector2 = this.target.position;
         if (Time.time < this.avoidOtherCarTime)
         {
             num     *= this.avoidOtherCarSlowdown;
             vector2 += this.target.right * this.avoidPathOffset;
         }
         else
         {
             vector2 += this.target.right * (Mathf.PerlinNoise(Time.time * this.lateralWanderSpeed, this.randomPerlin) * 2f - 1f) * this.lateralWanderDistance;
         }
         float num2 = (num >= this.carController.CurrentSpeed) ? this.accelSensitivity : this.brakeSensitivity;
         float num3 = Mathf.Clamp((num - this.carController.CurrentSpeed) * num2, -1f, 1f);
         num3 *= 1f - this.accelWanderAmount + Mathf.PerlinNoise(Time.time * this.accelWanderSpeed, this.randomPerlin) * this.accelWanderAmount;
         Vector3 vector3    = base.transform.InverseTransformPoint(vector2);
         float   num4       = Mathf.Atan2(vector3.x, vector3.z) * 57.29578f;
         float   steerInput = Mathf.Clamp(num4 * this.steerSensitivity, -1f, 1f) * Mathf.Sign(this.carController.CurrentSpeed);
         this.carController.Move(steerInput, num3);
         if (this.stopWhenTargetReached && vector3.magnitude < this.reachTargetThreshold)
         {
             this.driving = false;
         }
     }
 }
Example #4
0
        public override void handle(CarAIControl controller, Dictionary <int, VRAVEObstacle> obstacles, float currentSpeed, CarAIControl.BrakeCondition brakeCondition)
        {
            if (!Enable)
            {
                return;
            }
            else
            {
                if (obstacles.ContainsKey(9) && obstacles[9].obstacleTag.Equals(VRAVEStrings.Obstacle) && (obstacles[9].Distance <= 25))
                {
                    //Don't turn
                    Enable = false;
                    controller.TooClose        = false;
                    controller.TooFar          = false;
                    controller.AccelMultiplier = 0f;

                    //GetComponentInParent<CarController>().MaxSpeed = AISpeed;
                    return;
                }
                else if (obstacles.ContainsKey(7) && obstacles[7].obstacleTag.Equals(VRAVEStrings.AI_Car))
                {
                    CarController userCarController = GetComponentInParent <CarController>();
                    float         AISpeed           = obstacles[7].obstacle.collider.GetComponentInParent <CarController>().MaxSpeed;
                    float         differential      = obstacles[7].Distance - followDistance;
                    Debug.Log("Distance: " + obstacles[7].Distance + "   Differential: " + differential);
                    if (Mathf.Abs(differential) > 5f)
                    {
                        if (differential > 0f)  //too far away
                        {
                            //userCarController.MaxSpeed = 1.2f * AISpeed;
                            controller.AccelMultiplier = (1.0f + Mathf.Abs(controller.AccelMultiplier)) * 1.05f;
                            controller.TooFar          = true;
                            Debug.Log("Too far : " + controller.AccelMultiplier);
                        }
                        else if (differential < 0f) //(differential < 0)  //too close
                        {
                            //userCarController.MaxSpeed = 1.2f * AISpeed;
                            controller.AccelMultiplier = -1.0f + -1f * Mathf.Abs(controller.AccelMultiplier) * (1.0f / 1.05f);
                            //controller.AccelMultiplier = 0;
                            controller.TooClose = true;
                            Debug.Log("Too close : " + controller.AccelMultiplier);
                        }
                    }
                    else if (Math.Abs(differential) > 1f)
                    {
                        if (differential > 0f)  //too far away
                        {
                            userCarController.MaxSpeed = 1.5f * AISpeed;
                            //controller.AccelMultiplier = controller.AccelMultiplier / 2;
                            controller.TooFar = true;
                            Debug.Log("Too far : " + controller.AccelMultiplier);
                        }
                        else if (differential < 0f) //(differential < 0)  //too close
                        {
                            userCarController.MaxSpeed = 1.5f * AISpeed;
                            //controller.AccelMultiplier = controller.AccelMultiplier / 2;
                            controller.TooClose = true;
                            Debug.Log("Too close : " + controller.AccelMultiplier);
                        }
                    }
                    else
                    {
                        //In the correct following range. Do nothing.
                        controller.TooClose = false;
                        controller.TooFar   = false;

                        GetComponentInParent <CarController>().MaxSpeed = AISpeed;
                        //obstacles[7].obstacle.collider.GetComponentInParent<CarController>().MaxSpeed = AISpeed;
                    }
                }
            }
        }
Example #5
0
 // Defines how a vehicle should react to an obstacle hit
 //
 // In most cases, you will want to disable the sensor response handler at the end of handle()
 public abstract void handle(CarAIControl controller, Dictionary <int, VRObstacle> obstacles,
                             float currentSpeed,
                             CarAIControl.BrakeCondition brakeCondition);
Example #6
0
 public override void handle(CarAIControl controller, Dictionary <int, VRAVEObstacle> obstacles, float currentSpeed, CarAIControl.BrakeCondition brakeCondition)
 {
     if (!Enable)
     {
         return;
     }
     else
     {
         if (passingCheck(obstacles))
         {
             Enable = false;
             controller.IsPassing = true;
         }
     }
 }