イラスト、モデリング、Unity、VR関連

unityとかblenderとかvr関連の作業メモ

OculusGo タッチパッドで移動するとき、方向を限定するコード

    //移動方向の取得
    Vector3 GetMoveDirection(){
      Vector2 dir = OVRInput.Get(OVRInput.Axis2D.PrimaryTouchpad);
      //そのまま使うと方向がいい感じにならないので8方向に限定する
      float dig = GetDigree();
      float unit = 22.5f;

      if(CheckRange(dig,unit*15,360) || CheckRange(dig,0,unit)) return new Vector3(1,0,0);
      if(CheckRange(dig,unit,unit*3)) return new Vector3(1,0,1);
      if(CheckRange(dig,unit*3,unit*5)) return new Vector3(0,0,1);
      if(CheckRange(dig,unit*5,unit*7)) return new Vector3(-1,0,1);
      if(CheckRange(dig,unit*7,unit*9)) return new Vector3(-1,0,0);
      if(CheckRange(dig,unit*9,unit*11)) return new Vector3(-1,0,-1);
      if(CheckRange(dig,unit*11,unit*13)) return new Vector3(0,0,-1);
      if(CheckRange(dig,unit*13,unit*15)) return new Vector3(1,0,-1);
      return Vector3.zero;
    }

    //X軸と座標ベクトルとの角度
    float GetDigree(){
      Vector2 dir = OVRInput.Get(OVRInput.Axis2D.PrimaryTouchpad);
      var dig = Mathf.Atan2(dir.y,dir.x)*Mathf.Rad2Deg;
      if(dig<0) dig += 360;
      return dig;
    }

    bool CheckRange(float val,float from,float to){
      return val>=from && val<=to ? true:false;
    }