LoginSignup
1
1

More than 3 years have passed since last update.

Questのアナログスティックを使ってオブジェクトを回転させる方法 備忘録

Posted at

アナログスティックを使ってクルクルさせたいがために作りました。

int saveRotation = 0;
int currentRotation = 0;
float dragAngle = 0;
int lastSavePoint = 0;

void Update()
{
    if (OVRInput.GetDown(OVRInput.Button.PrimaryThumstickUp)
       || OVRInput.GetDown(OVRInput.Button.PrimaryThumstickRight)
       || OVRInput.GetDown(OVRInput.Button.PrimaryThumstickDown)
       || OVRInput.GetDown(OVRInput.Button.PrimaryThumstickLeft))
    {
        Vector2 pos = Vector2.zero;
        dragAngle = Mathf.Atan2(pos.y, pos.x) * Mathf.Rad2Deg;
        currentRotation = saveRotation;
    }

    if (OVRInput.Get(OVRInput.Button.PrimaryThumstickUp)
       || OVRInput.Get(OVRInput.Button.PrimaryThumstickRight)
       || OVRInput.Get(OVRInput.Button.PrimaryThumstickDown)
       || OVRInput.Get(OVRInput.Button.PrimaryThumstickLeft))
    {
        Vector2 pos = OVRInput.Get(OVRInput.Axis2D.PrimaryThumbstick);
        int rotation = -(int)((Mathf.Atan2(pos.y, pos.x) * Mathf.Rad2Deg) - dragAngle);
        currentRotation = rotation + saveRotation;
        transform.localRotation = Quaternion.Euler(0, 0, -currentRotation);
        if (currentRotation > lastSavePoint && currentRotation - lastSavePoint == 1)
        {
            // 角度が上がった時の処理
        }

        if (lastSavePoint > currentRotation && lastSavePoint - currentRotation == 1)
        {
            // 角度が下がった時の処理
        }
    }

    if (OVRInput.GetUp(OVRInput.Button.PrimaryThumstickUp)
       || OVRInput.GetUp(OVRInput.Button.PrimaryThumstickRight)
       || OVRInput.GetUp(OVRInput.Button.PrimaryThumstickDown)
       || OVRInput.GetUp(OVRInput.Button.PrimaryThumstickLeft))
    {
        saveRotation = currentRotation;
        transform.localRotation = Quaternion.Euler(0, 0, currentRotation);
    }
}
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1