void FixedUpdate()
        {
            // Get constant inputs
            if (!string.IsNullOrEmpty(accelAxis))
            {
                vp.SetAccel(Input.GetAxis(accelAxis));
            }

            if (!string.IsNullOrEmpty(brakeAxis))
            {
                vp.SetBrake(Input.GetAxis(brakeAxis));
            }

            if (!string.IsNullOrEmpty(steerAxis))
            {
                vp.SetSteer(Input.GetAxis(steerAxis));
            }

            if (!string.IsNullOrEmpty(ebrakeAxis))
            {
                vp.SetEbrake(Input.GetAxis(ebrakeAxis));
            }

            if (!string.IsNullOrEmpty(boostButton))
            {
                vp.SetBoost(Input.GetButton(boostButton));
            }

            if (!string.IsNullOrEmpty(pitchAxis))
            {
                vp.SetPitch(Input.GetAxis(pitchAxis));
            }

            if (!string.IsNullOrEmpty(yawAxis))
            {
                vp.SetYaw(Input.GetAxis(yawAxis));
            }

            if (!string.IsNullOrEmpty(rollAxis))
            {
                vp.SetRoll(Input.GetAxis(rollAxis));
            }

            if (!string.IsNullOrEmpty(upshiftButton))
            {
                vp.SetUpshift(Input.GetAxis(upshiftButton));
            }

            if (!string.IsNullOrEmpty(downshiftButton))
            {
                vp.SetDownshift(Input.GetAxis(downshiftButton));
            }
        }
Пример #2
0
        void FixedUpdate()
        {
            if (setter)
            {
                accelerationDelta = Input.acceleration - accelerationPrev;
                accelerationPrev  = Input.acceleration;
                vp.SetAccel(setter.accel);
                vp.SetBrake(setter.brake);
                vp.SetEbrake(setter.ebrake);
                vp.SetBoost(setter.boost);

                if (useAccelerometer)
                {
                    vp.SetSteer((Input.acceleration.x + accelerationDelta.x * deltaFactor) * steerFactor);
                    vp.SetYaw(Input.acceleration.x * flipFactor);
                    vp.SetPitch(-Input.acceleration.z * flipFactor);
                }
                else
                {
                    vp.SetSteer(setter.steer);
                }
            }
        }
        void FixedUpdate()
        {
            if (target)
            {
                if (target != targetPrev)
                {
                    InitializeTarget();
                }

                targetPrev = target;

                //Is the target a waypoint?
                targetIsWaypoint = target.GetComponent <VehicleWaypoint>();
                //Can I see the target?
                targetVisible = !Physics.Linecast(tr.position, target.position, viewBlockMask);

                if (targetVisible || targetIsWaypoint)
                {
                    targetPoint = targetBody ? target.position + targetBody.velocity : target.position;
                }

                if (targetIsWaypoint)
                {
                    //if vehicle is close enough to target waypoint, switch to the next one
                    if ((tr.position - target.position).sqrMagnitude <= targetWaypoint.radius * targetWaypoint.radius)
                    {
                        target         = targetWaypoint.nextPoint.transform;
                        targetWaypoint = targetWaypoint.nextPoint;
                        prevSpeed      = speed;
                        speed          = Mathf.Clamp01(targetWaypoint.speed * initialSpeed);
                        brakeTime      = prevSpeed / speed;

                        if (brakeTime <= 1)
                        {
                            brakeTime = 0;
                        }
                    }
                }

                brakeTime = Mathf.Max(0, brakeTime - Time.fixedDeltaTime);
                //Is the distance to the target less than the follow distance?
                close       = (tr.position - target.position).sqrMagnitude <= Mathf.Pow(followDistance, 2) && !targetIsWaypoint;
                dirToTarget = (targetPoint - tr.position).normalized;
                lookDot     = Vector3.Dot(vp.forwardDir, dirToTarget);
                steerDot    = Vector3.Dot(vp.rightDir, dirToTarget);

                //Attempt to reverse if vehicle is stuck
                stoppedTime = Mathf.Abs(vp.localVelocity.z) < 1 && !close && vp.groundedWheels > 0 ? stoppedTime + Time.fixedDeltaTime : 0;

                if (stoppedTime > stopTimeReverse && reverseTime == 0)
                {
                    reverseTime = reverseAttemptTime;
                    reverseAttempts++;
                }

                //Reset if reversed too many times
                if (reverseAttempts > resetReverseCount && resetReverseCount >= 0)
                {
                    StartCoroutine(ReverseReset());
                }

                reverseTime = Mathf.Max(0, reverseTime - Time.fixedDeltaTime);

                if (targetVelocity > 0)
                {
                    speedLimit = Mathf.Clamp01(targetVelocity - vp.localVelocity.z);
                }
                else
                {
                    speedLimit = 1;
                }

                //Set vehicle inputs
                vp.SetAccel(!close && (lookDot > 0 || vp.localVelocity.z < 5) && vp.groundedWheels > 0 && reverseTime == 0 ? speed * speedLimit : 0);
                vp.SetBrake(reverseTime == 0 && brakeTime == 0 && !(close && vp.localVelocity.z > 0.1f) ? (lookDot < 0.5f && lookDot > 0 && vp.localVelocity.z > 10 ? 0.5f - lookDot : 0) : (reverseTime > 0 ? 1 : (brakeTime > 0 ? brakeTime * 0.2f : 1 - Mathf.Clamp01(Vector3.Distance(tr.position, target.position) / Mathf.Max(0.01f, followDistance)))));
                vp.SetSteer(reverseTime == 0 ? Mathf.Abs(Mathf.Pow(steerDot, (tr.position - target.position).sqrMagnitude > 20 ? 1 : 2)) * Mathf.Sign(steerDot) : -Mathf.Sign(steerDot) * (close ? 0 : 1));
                vp.SetEbrake((close && vp.localVelocity.z <= 0.1f) || (lookDot <= 0 && vp.velMag > 20) ? 1 : 0);
            }

            rolledOverTime = va.rolledOver ? rolledOverTime + Time.fixedDeltaTime : 0;

            //Reset if stuck rolled over
            if (rolledOverTime > rollResetTime && rollResetTime >= 0)
            {
                StartCoroutine(ResetRotation());
            }
        }