Ejemplo n.º 1
0
    private void UpdateControls(Vector3 aim)
    {
        // Determine angle between user gaze vector and helicopter forward, in xz
        // plane
        Vector3 view    = new Vector3(Camera.main.transform.forward.x, 0, Camera.main.transform.forward.z);
        Vector3 forward = new Vector3(transform.forward.x, 0, transform.forward.z);
        float   angle   = Vector3.Angle(view, forward);

        // Get current joypad axis values
#if UNITY_EDITOR
        float hor     = Input.GetAxis("Horizontal");
        float ver     = Input.GetAxis("Vertical");
        float hor2    = Input.GetAxis("Horizontal2");
        float ver2    = -Input.GetAxis("Vertical2");
        float lt      = Input.GetAxis("Axis9");
        float rt      = Input.GetAxis("Axis10");
        bool  buttonA = Input.GetKey(KeyCode.Joystick1Button0);
        bool  buttonB = Input.GetKey(KeyCode.Joystick1Button1);
        bool  fire    = rt > 0.5f || buttonA;
#else
        m_xboxController.Update();
        float hor     = m_xboxController.GetAxisLeftThumbstickX();
        float ver     = m_xboxController.GetAxisLeftThumbstickY();
        float hor2    = m_xboxController.GetAxisRightThumbstickX();
        float ver2    = m_xboxController.GetAxisRightThumbstickY();
        float lt      = m_xboxController.GetAxisLeftTrigger();
        float rt      = m_xboxController.GetAxisRightTrigger();
        bool  fire    = rt > 0.5f;
        bool  buttonA = m_xboxController.GetButton(ControllerButton.A);
        bool  buttonB = m_xboxController.GetButton(ControllerButton.B);

        /*
         * float hor = Input.GetAxis("Horizontal");
         * float ver = Input.GetAxis("Vertical");
         * float axis3 = Input.GetAxis("Axis3");
         * float lt = Mathf.Max(axis3, 0);
         * float rt = -Mathf.Min(axis3, 0);
         */
#endif

        // Update our control axes
        ReorientAxes(hor, ver);

        // Apply longitudinal and lateral controls. Compute projection of joypad
        // lateral/longitudinal axes onto helicopter's.
        float joypadLongitudinalToHeliLongitudinal = Vector3.Dot(m_joypadLongitudinalAxis, transform.forward);
        float joypadLongitudinalToHeliLateral      = Vector3.Dot(m_joypadLongitudinalAxis, transform.right);
        float joypadLateralToHeliLongitudinal      = Vector3.Dot(m_joypadLateralAxis, transform.forward);
        float joypadLateralToHeliLateral           = Vector3.Dot(m_joypadLateralAxis, transform.right);
        m_controls.longitudinal = joypadLongitudinalToHeliLongitudinal * ver + joypadLateralToHeliLongitudinal * hor;
        m_controls.lateral      = joypadLongitudinalToHeliLateral * ver + joypadLateralToHeliLateral * hor;

        // Helicopter rotation
        m_controls.rotational = hor2;

        // Altitude control (trigger axes each range from 0 to 1)
        m_controls.altitude = ver2;

        // Gun
        if (fire)
        {
            m_helicopter.FireGun();
        }

        /*
         * if (fire && (Time.time - m_gunLastFired >= GUN_FIRE_PERIOD))
         * {
         * FireGun(aim);
         * }
         */
    }