public static Quaternion Recenter(bool isLeftJoyCon)
    {
        if (m_joyconL == null && m_joyconR == null)
        {
            Debug.LogWarning("Abort Recenter: No joycon found");
            return(Quaternion.identity);
        }

        if (isLeftJoyCon)
        {
            m_joyconL?.Recenter();
        }
        else
        {
            m_joyconR?.Recenter();
        }
        return(GetJoyConOrientation(isLeftJoyCon));
    }
示例#2
0
    // Update is called once per frame
    void Update()
    {
        //make sure the Joycon only gets checked if attached
        if (joycons.Count > 0)
        {
            Joycon joycon = joycons[joyconIndex];
            //GetButtonDown checks if a button has been pressed (not held)
            if (joycon.GetButtonDown(Joycon.Button.SHOULDER_1))
            {
                Debug.Log("Shoulder button 1 pressed");
                joycon.SetRumble(0, 0, 0);
            }
            if (joycon.GetButtonDown(Joycon.Button.SHOULDER_2))
            {
                Debug.Log("Shoulder button 2 pressed");

                //Joycon has no magnetometer, so it cannot accurately determine its yaw value.
                //Joycon.Recenter allows the user to reset the yaw value.
                joycon.Recenter();
                Debug.Log("Joycon recentered");
            }
            if (joycon.GetButtonDown(Joycon.Button.DPAD_UP))
            {
                Debug.Log("DPad_up or X button pressed");
            }
            if (joycon.GetButtonDown(Joycon.Button.DPAD_RIGHT))
            {
                Debug.Log("DPad right or A button pressed");
            }
            if (joycon.GetButtonDown(Joycon.Button.DPAD_DOWN))
            {
                Debug.Log("DPad_down or B button pressed");
            }
            if (joycon.GetButtonDown(Joycon.Button.DPAD_LEFT))
            {
                Debug.Log("DPad left or Y button pressed");
            }
            if (joycon.GetButtonDown(Joycon.Button.HOME))
            {
                Debug.Log("Home button pressed");
            }
            if (joycon.GetButtonDown(Joycon.Button.PLUS))
            {
                Debug.Log("Plus button pressed");
                Debug.Log("Rumble");

                if (rumbleDuration > 0)
                {
                    joycon.SetRumble(lowFrequency, highFrequency, amplitude, rumbleDuration);
                }
                else
                {
                    joycon.SetRumble(lowFrequency, highFrequency, amplitude);
                }

                // The last argument (time) in SetRumble is optional.
                // Call it with three arguments to turn it on without telling it when to turn off.
                // (Useful for dynamically changing rumble values.)
                // Then call SetRumble(0,0,0) when you want to turn it off.
            }
            if (joycon.GetButtonDown(Joycon.Button.STICK))
            {
                Debug.Log("Stick pressed");
            }
            if (joycon.GetButtonDown(Joycon.Button.SR))
            {
                Debug.Log("SR button pressed");
            }
            if (joycon.GetButtonDown(Joycon.Button.SL))
            {
                Debug.Log("SL button pressed");
            }


            stick = joycon.GetStick();

            //Gyro values: x, y, z axis values (in radians per second)
            gyro = joycon.GetGyro();

            //Accel values: x, y, z axis values (in Gs)
            accel = joycon.GetAccel();

            orientation  = joycon.GetVector();
            orientation *= Quaternion.Euler(Vector3.right * 180);
            orientation  = new Quaternion(-orientation.x, orientation.y, orientation.z, orientation.w);
            gameObject.transform.localRotation = Quaternion.Inverse(orientation);

            gameObject.transform.rotation = orientation;

            if (accel.magnitude > 0)
            {
                gameObject.transform.Translate(accel * Time.deltaTime);
            }
        }
    }