private void FixedUpdate()
        {
            // pass the input to the car!

            Vector2 input = OVRInput.Get(OVRInput.Axis2D.PrimaryThumbstick);

            car.Move(input.x, input.y);
        }
Exemple #2
0
        private void FixedUpdate()
        {
            // pass the input to the car!
            float h = Input.GetAxis("Horizontal");
            float v = Input.GetAxis("Vertical");

            car.Move(h, v);
        }
        private void FixedUpdate()
        {
            // pass the input to the car!
            float h = CrossPlatformInputManager.GetAxis("Horizontal");
            float v = CrossPlatformInputManager.GetAxis("Vertical");

            car.Move(h, v);
        }
Exemple #4
0
 private void FixedUpdate()
 {
     // pass the input to the car!
     //horizontal = CrossPlatformInputManager.GetAxis("Horizontal");
     //vertical = CrossPlatformInputManager.GetAxis("Vertical");
     vertical = 0.1f;
     car.Move(horizontal, vertical);
 }
Exemple #5
0
        private void FixedUpdate()
        {
            // pass the input to the car!
            //float h = CrossPlatformInputManager.GetAxis("Horizontal");
            //float v = CrossPlatformInputManager.GetAxis("Vertical");

            float vGamePad = CrossPlatformInputManager.GetAxis("Fire1");

            car.Move(carTurn, vGamePad);

            //print (h + " " + vGamePad);
        }
Exemple #6
0
        private void FixedUpdate()
        {
            if (target == null || !driving)
            {
                // Car should not be moving,
                // (so use accel/brake to get to zero speed)
                float accel = Mathf.Clamp(-carController.CurrentSpeed, -1, 1);
                carController.Move(0, accel);
            }
            else
            {
                Vector3 fwd = transform.forward;
                if (GetComponent <Rigidbody>().velocity.magnitude > carController.MaxSpeed * 0.1f)
                {
                    fwd = GetComponent <Rigidbody>().velocity;
                }

                float desiredSpeed = carController.MaxSpeed;

                // now it's time to decide if we should be slowing down...
                switch (brakeCondition)
                {
                case BrakeCondition.TargetDirectionDifference:
                {
                    // the car will brake according to the upcoming change in direction of the target. Useful for route-based AI, slowing for corners.

                    // check out the angle of our target compared to the current direction of the car
                    float approachingCornerAngle = Vector3.Angle(target.forward, fwd);

                    // also consider the current amount we're turning, multiplied up and then compared in the same way as an upcoming corner angle
                    float spinningAngle = GetComponent <Rigidbody>().angularVelocity.magnitude *cautiousAngularVelocityFactor;

                    // if it's different to our current angle, we need to be cautious (i.e. slow down) a certain amount
                    float cautiousnessRequired = Mathf.InverseLerp(0, cautiousMaxAngle,
                                                                   Mathf.Max(spinningAngle,
                                                                             approachingCornerAngle));
                    desiredSpeed = Mathf.Lerp(carController.MaxSpeed, carController.MaxSpeed * cautiousSpeedFactor,
                                              cautiousnessRequired);
                    break;
                }

                case BrakeCondition.TargetDistance:
                {
                    // the car will brake as it approaches its target, regardless of the target's direction. Useful if you want the car to
                    // head for a stationary target and come to rest when it arrives there.

                    // check out the distance to target
                    Vector3 delta = target.position - transform.position;
                    float   distanceCautiousFactor = Mathf.InverseLerp(cautiousMaxDistance, 0, delta.magnitude);

                    // also consider the current amount we're turning, multiplied up and then compared in the same way as an upcoming corner angle
                    float spinningAngle = GetComponent <Rigidbody>().angularVelocity.magnitude *cautiousAngularVelocityFactor;

                    // if it's different to our current angle, we need to be cautious (i.e. slow down) a certain amount
                    float cautiousnessRequired = Mathf.Max(
                        Mathf.InverseLerp(0, cautiousMaxAngle, spinningAngle), distanceCautiousFactor);
                    desiredSpeed = Mathf.Lerp(carController.MaxSpeed, carController.MaxSpeed * cautiousSpeedFactor,
                                              cautiousnessRequired);
                    break;
                }

                case BrakeCondition.NeverBrake:
                    break;     // blarg!
                }

                // Evasive action due to collision with other cars:

                // our target position starts off as the 'real' target position
                Vector3 offsetTargetPos = target.position;

                // if are we currently taking evasive action to prevent being stuck against another car:
                if (Time.time < avoidOtherCarTime)
                {
                    // slow down if necessary (if we were behind the other car when collision occured)
                    desiredSpeed *= avoidOtherCarSlowdown;

                    // and veer towards the side of our path-to-target that is away from the other car
                    offsetTargetPos += target.right * avoidPathOffset;
                }
                else
                {
                    // no need for evasive action, we can just wander across the path-to-target in a random way,
                    // which can help prevent AI from seeming too uniform and robotic in their driving
                    offsetTargetPos += target.right *
                                       (Mathf.PerlinNoise(Time.time * lateralWanderSpeed, randomPerlin) * 2 - 1) *
                                       lateralWanderDistance;
                }

                // use different sensitivity depending on whether accelerating or braking:
                float accelBrakeSensitivity = (desiredSpeed < carController.CurrentSpeed)
                                                  ? brakeSensitivity
                                                  : accelSensitivity;

                // decide the actual amount of accel/brake input to achieve desired speed.
                float accel = Mathf.Clamp((desiredSpeed - carController.CurrentSpeed) * accelBrakeSensitivity, -1, 1);

                // add acceleration 'wander', which also prevents AI from seeming too uniform and robotic in their driving
                // i.e. increasing the accel wander amount can introduce jostling and bumps between AI cars in a race
                accel *= (1 - accelWanderAmount) +
                         (Mathf.PerlinNoise(Time.time * accelWanderSpeed, randomPerlin) * accelWanderAmount);

                // calculate the local-relative position of the target, to steer towards
                Vector3 localTarget = transform.InverseTransformPoint(offsetTargetPos);

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

                // get the amount of steering needed to aim the car towards the target
                float steer = Mathf.Clamp(targetAngle * steerSensitivity, -1, 1) * Mathf.Sign(carController.CurrentSpeed);

                // feed input to the car controller.
                carController.Move(steer, accel);

                // if appropriate, stop driving when we're close enough to the target.
                if (stopWhenTargetReached && localTarget.magnitude < reachTargetThreshold)
                {
                    driving = false;
                }
            }
        }