示例#1
0
    protected virtual void Update()
    {
#if UNITY_PS4 && !UNITY_EDITOR
        // HACK still not fixed in Unity 5.4
        if (mPS4RenderScale != 0)
        {
            mPS4RenderScale--;
            UnityEngine.VR.VRSettings.renderScale = 1.4f;
        }
#endif

        // Quit on ESCAPE
        if (Input.GetKey("escape"))
        {
            Application.Quit();
        }

#if VZ_GAME
        // Get replay record
        if (VZReplay.Playback())
        {
            VZReplay.Record record = VZReplay.Instance.GetRecord();

            if (record != null)
            {
                InputSpeed = record.inputSpeed;

                LeftButton.Set(record.leftButton);
                RightButton.Set(record.rightButton);
                DpadUp.Set(record.dpadUp);
                DpadDown.Set(record.dpadDown);
                DpadLeft.Set(record.dpadLeft);
                DpadRight.Set(record.dpadRight);
                RightUp.Set(record.rightUp);
                RightDown.Set(record.rightDown);
                RightLeft.Set(record.rightLeft);
                RightRight.Set(record.rightRight);

                mCamera.localRotation = record.headRotation;
                mCamera.localPosition = record.headPosition;
            }
        }
        else
#endif
        {
            // Get speed and buttons
            if (mBikeState.Type >= 0)
            {
                VZPlugin.UpdateBike(ref mBikeState, Time.time);

                InputSpeed = mBikeState.Speed;

                LeftButton.Set(mBikeState.LeftTrigger);
                RightButton.Set(mBikeState.RightTrigger);
                DpadUp.Set(mBikeState.DpadUp);
                DpadDown.Set(mBikeState.DpadDown);
                DpadLeft.Set(mBikeState.DpadLeft);
                DpadRight.Set(mBikeState.DpadRight);
                RightUp.Set(mBikeState.RightUp);
                RightDown.Set(mBikeState.RightDown);
                RightLeft.Set(mBikeState.RightLeft);
                RightRight.Set(mBikeState.RightRight);
            }
#if UNITY_EDITOR || UNITY_PS4
            else
            {
                // Read from joypad/keyboard if no serial
                InputSpeed = ControllerSpeedAxis() * kControllerMaxSpeed;
                LeftButton.Set(ControllerLeftButton());
                RightButton.Set(ControllerRightButton());
            }
#endif

            // Rotate head without hmd
            if (!mHasHmd)
            {
                mControllerYaw           = Mathf.SmoothDamp(mControllerYaw, ControllerYawAxis() * 2 * Mathf.Rad2Deg, ref mControllerYawVel, 1);
                mControllerPitch         = Mathf.SmoothDamp(mControllerPitch, ControllerPitchAxis() * Mathf.Rad2Deg, ref mControllerPitchVel, 1);
                mCamera.localEulerAngles = new Vector3(mControllerPitch, mControllerYaw, 0);

                //Debug.Log(string.Format("yaw = {0}, pitch = {1}", ControllerYawAxis(), ControllerPitchAxis()));
            }

            // Translate head without hmd
            if (!mHasHmd)
            {
                mControllerLean = Mathf.SmoothDamp(mControllerLean, ControllerLean() / 4, ref mControllerLeanVel, 0.5f);
                mControllerBend = Mathf.SmoothDamp(mControllerBend, Input.GetAxis("bend") / 4, ref mControllerBendVel, 0.5f);

                //Debug.Log(string.Format("lean = {0}, bend = {1}", ControllerLean(), Input.GetAxis("bend")));

                Vector3 pos = mCamera.localPosition;
                pos.x = mControllerLean;
                pos.z = mControllerBend;
                mCamera.localPosition = pos;
            }
#if VZ_EMULATE_GEARVR
            else if (mBikeState.Type >= 0)
            {
                float lean = 0;
                float bend = 0;

                if (mBikeState.DpadLeft)
                {
                    lean = -1;
                }
                else if (mBikeState.DpadRight)
                {
                    lean = 1;
                }
                if (mBikeState.DpadUp)
                {
                    bend = 1;
                }
                else if (mBikeState.DpadDown)
                {
                    bend = -1;
                }

                mControllerLean = Mathf.SmoothDamp(mControllerLean, lean / 4, ref mControllerLeanVel, 0.5f);
                mControllerBend = Mathf.SmoothDamp(mControllerBend, bend / 4, ref mControllerBendVel, 0.5f);

                Vector3 pos = mCamera.localPosition;
                pos.x = mControllerLean;
                pos.z = mControllerBend;
                mCamera.localPosition = pos;
            }
#endif
        }

        // Update Head from Camera and CameraOffset
        float ang    = -mCameraOffset.localEulerAngles.y * Mathf.Deg2Rad;
        float sinAng = Mathf.Sin(ang);
        float cosAng = Mathf.Cos(ang);
        float x      = -mCamera.localPosition.z * sinAng + mCamera.localPosition.x * cosAng + mCameraOffset.localPosition.x;
        float z      = mCamera.localPosition.z * cosAng + mCamera.localPosition.x * sinAng + mCameraOffset.localPosition.z;
        float y      = mCamera.localPosition.y + mCameraOffset.localPosition.y;

        Head.localPosition = new Vector3(x, y, z);

        Vector3 localAngles = mCamera.localEulerAngles;
        localAngles.y        += mCameraOffset.localEulerAngles.y;
        Head.localEulerAngles = localAngles;

        // Update button data
        LeftButton.Update();
        RightButton.Update();
        DpadUp.Update();
        DpadDown.Update();
        DpadLeft.Update();
        DpadRight.Update();
        RightUp.Update();
        RightDown.Update();
        RightLeft.Update();
        RightRight.Update();

        // Get head rot
        HeadRot = -Head.localEulerAngles.y * Mathf.Deg2Rad;

        if (HeadRot < -Mathf.PI)
        {
            HeadRot += Mathf.PI * 2;
        }

        // Get head lean & bend
        HeadLean = -Head.localPosition.x;
        HeadBend = Head.localPosition.z;

        // Subtract rot from lean/bend if hmd (controller doesn't simulate head width)
        if (mHasHmd)
        {
#if UNITY_PS4 && !UNITY_EDITOR
            /* PS4 seems to move less
             * HeadLean *= 1.25f;
             * HeadBend *= 1.50f;
             */
#endif

            // Adjust lean
            if (Mathf.Abs(HeadLean) < kHeadDead)
            {
                HeadLean = 0;
            }
            else if (HeadLean > 0)
            {
                if (HeadRot > Mathf.PI / 2.0f)
                {
                    HeadLean -= kHeadWidth;
                }
                else
                {
                    HeadLean -= kHeadWidth * Mathf.Sin(HeadRot);
                }

                HeadLean -= kHeadDead;

                if (HeadLean < 0)
                {
                    HeadLean = 0;
                }
            }
            else
            {
                if (HeadRot < -Mathf.PI / 2.0f)
                {
                    HeadLean += kHeadWidth;
                }
                else
                {
                    HeadLean -= kHeadWidth * Mathf.Sin(HeadRot);
                }

                HeadLean += kHeadDead;

                if (HeadLean > 0)
                {
                    HeadLean = 0;
                }
            }

            // Adjust bend
            float headPitch = Head.localEulerAngles.x * Mathf.Deg2Rad;

            if (headPitch < -Mathf.PI)
            {
                headPitch += Mathf.PI * 2;
            }

            HeadBend += kHeadWidth * (1 - Mathf.Cos(headPitch));
        }

        // Update distance

        // Adjust "real" speed by resistance factor, which isn't yet incorporated into InputSpeed because all
        // current games don't want it there
        float realSpeed = InputSpeed * ResistanceFactor();

        Distance += Mathf.Abs(realSpeed) * Time.deltaTime;
    }
示例#2
0
    protected virtual void UpdateNormal()
    {
        // Fill input
        mInput.DraftSpeed          = DraftSpeed;
        mInput.DraftFactor         = DraftFactor;
        mInput.DownhillFactor      = DownhillFactor;
        mInput.UphillFactor        = UphillFactor;
        mInput.ControllerHeadLean  = Controller.HeadLean;
        mInput.LeanFudge           = LeanFudge;
        mInput.ApparentLean        = LeanIn;
        mInput.SpeedFudge          = SpeedFudge;
        mInput.MaxTurn             = MaxTurn;
        mInput.StoppedTurnFraction = SlowRotateLimit;
        mInput.AllowDrift          = AllowDrift;
        mInput.AllowYaw            = AllowRotate;
        mInput.AllowPitch          = AllowPitch;
        mInput.AllowRoll           = AllowRoll;
        mInput.RotateAtStop        = RotateAtStop;
        mInput.ControllerHeadRot   = Controller.HeadRot;
        mInput.Colliding           = mColliding;
        mInput.BodyRot             = mBodyRot;
        mInput.Scale                           = mScale;
        mInput.LandingHardness                 = LandingHardness;
        mInput.LandingRadius                   = LandingRadius;
        mInput.ControllerInputSpeed            = Controller.InputSpeed;
        mInput.SpeedMultiplier                 = mSpeedMultiplier;
        mInput.SpeedMultiplierSpeedSettleTime  = mSpeedMultiplierSpeedSettleTime;
        mInput.SpeedSettleTimeWhenAccelerating = mSpeedSettleTimeWhenAccelerating;
        mInput.SpeedSettleTimeWhenBraking      = mSpeedSettleTimeWhenBraking;
        mInput.MaxSpeed                        = MaxSpeed;
        mInput.MaxVertSpeed                    = MaxVertSpeed;
        mInput.Reverse                         = Reverse;
        mInput.DeltaTime                       = Time.deltaTime;
        mInput.PlayerX                         = transform.position.x;
        mInput.PlayerY                         = transform.position.y;
        mInput.PlayerZ                         = transform.position.z;

        Vector3 velocity = GetComponent <Rigidbody>().velocity;

        mInput.VelocityX = velocity.x;
        mInput.VelocityY = velocity.y;
        mInput.VelocityZ = velocity.z;

        Vector3    rayOrigin = transform.position + transform.TransformVector(mRaycastOffset);
        Vector3    rayDir    = Vector3.down;
        RaycastHit hit;

        mInput.RaycastResult = Physics.Raycast(rayOrigin, rayDir, out hit, 1000.0f, mRaycastMask);

        if (mInput.RaycastResult)
        {
            mColliderInfo      = hit.collider.GetComponent <VZColliderInfo>();
            mInput.HitDistance = hit.distance;
            mInput.HitNormalX  = hit.normal.x;
            mInput.HitNormalY  = hit.normal.y;
            mInput.HitNormalZ  = hit.normal.z;
        }
        else
        {
            mColliderInfo = null;
        }

        if (mColliderInfo != null)
        {
            mInput.ColliderSpeedMultiplier = mColliderInfo.SpeedMultiplier;
        }
        else
        {
            mInput.ColliderSpeedMultiplier = 1;
        }

        // Move state
        VZPlugin.UpdateMotion(ref mInput, ref mOutput);

        // Calculate our power
        const float kMass = 68.0f; // todo: get this from the user

        mPower = CalculatePower(Time.deltaTime, kMass, mSpeed, mOutput.Speed);

        // Handle output
        mBodyRot = mOutput.BodyRot;
        mRotVel  = mOutput.RotVel;
        mTurn    = mOutput.Turn;
        mSpeed   = mOutput.Speed;
        mLean    = mOutput.Lean;

        if (mOutput.LandingSmoothed)
        {
            LandingSmoothed();
        }

        transform.eulerAngles = new Vector3(mOutput.TransformEulerX, mOutput.TransformEulerY, mOutput.TransformEulerZ);

        transform.position = new Vector3(mOutput.PlayerX, mOutput.PlayerY, mOutput.PlayerZ);

        // Update velocity
        GetComponent <Rigidbody>().velocity = new Vector3(mOutput.VelocityX, mOutput.VelocityY, mOutput.VelocityZ);

#if VZ_GAME && !UNITY_ANDROID
        // Override with replay
        if (VZReplay.Playback())
        {
            VZReplay.Record record = VZReplay.Instance.GetRecord();

            if (record != null)
            {
                GetComponent <Rigidbody>().velocity = record.playerVelocity;
                mBodyRot = record.bodyRot;

                if (VZReplay.Instance.OverridePlayer)
                {
                    transform.position = record.playerPosition;
                    transform.rotation = record.playerRotation;
                }
            }
        }
#endif

        // Keep neck vertical
        UpdateNeck();
    }
示例#3
0
    void Start()
    {
        InputSpeed = 0;
        HeadRot    = 0;
        HeadLean   = 0;
        HeadBend   = 0;

#if !UNITY_PS4 && VZ_GAME
        // Redirect Fmod to Rift headphones
        if (GameObject.Find("FMOD_StudioSystem") != null)
        {
            FMOD.System sys = FMODUnity.RuntimeManager.LowlevelSystem;
//         Debug.Log(sys);

            int num;
            sys.getNumDrivers(out num);
//         Debug.Log(num);

            for (int i = 0; i < num; ++i)
            {
                int              namelen = 100;
                StringBuilder    name    = new StringBuilder(namelen);
                Guid             guid;
                int              systemrate;
                FMOD.SPEAKERMODE speakermode;
                int              speakermodechannels;

                sys.getDriverInfo(i, name, namelen, out guid, out systemrate, out speakermode, out speakermodechannels);

//            Debug.Log(i + " " + name + " " + namelen + " " + guid + " " + systemrate + " " + speakermode + " " + speakermodechannels);

                if (name.ToString() == "Headphones (Rift Audio)")
                {
                    sys.setDriver(i);
                    Debug.Log("Redirecting Fmod audio to Rift headphones");
                    break;
                }
            }
        }
#endif

        // Setup hmd
#if VZ_GAME
        if (VZReplay.Playback())
        {
            mHasHmd = false;
            UnityEngine.VR.VRSettings.enabled = false;

            if (VZReplay.Instance.Camera != null)
            {
                Camera().enabled = false;
            }
        }
        else
#endif
        {
            mHasHmd = UnityEngine.VR.VRDevice.isPresent;
            UnityEngine.VR.VRSettings.enabled = mHasHmd;

#if UNITY_PS4 && !UNITY_EDITOR
            HmdSetupDialog.OpenAsync(0, PSVRCompleted);
#endif
        }

        // Try serial communication
#if VZ_GAME
        if (VZReplay.Playback())
        {
            mBikeState.Type = (int)VZBikeType.None;
        }
        else
#endif
        {
            TryConnectBike();
        }

        // Determine controller for other functions
        mController = ControllerType.Keyboard;

        string[] joysticks = Input.GetJoystickNames();

        if (joysticks.Length > 0)
        {
//			Debug.Log("Joystick = " + joysticks[0]);

            if (joysticks[0] == "Logitech Dual Action")
            {
                mController = ControllerType.Logitech;
            }
            else if (joysticks[0] == "Controller (Xbox One For Windows)")
            {
                //Debug.Log("Xbox controller selected.");
                mController = ControllerType.Xbox;
            }
            else if (joysticks[0] == "Controller (XBOX 360 For Windows)")
            {
                //Debug.Log("Xbox 360 controller selected.");
                mController = ControllerType.X360;
            }
#if UNITY_PS4 && !UNITY_EDITOR
            else if (joysticks[0] != "")
#else
            else if (joysticks[0] == "Wireless Controller")
#endif
            {
                mController = ControllerType.DS4;
            }
        }

        // Log
        String hmd = "None";
        if (mHasHmd)
        {
            if (IsSteamVR)
            {
                hmd = "SteamVR";
            }
            else
            {
#if UNITY_PS4 && !UNITY_EDITOR
                hmd = "PSVR";
#else
                hmd = "Oculus";
#endif
            }
        }
        Debug.Log("Bike:" + mBikeState.Type + " Hmd:" + hmd + " Controller:" + mController);
    }