tag:crieit.net,2005:https://crieit.net/tags/ARKit/feed 「ARKit」の記事 - Crieit Crieitでタグ「ARKit」に投稿された最近の記事 2021-01-11T18:26:32+09:00 https://crieit.net/tags/ARKit/feed tag:crieit.net,2005:PublicArticle/16355 2020-12-13T20:20:57+09:00 2021-01-11T18:26:32+09:00 https://crieit.net/posts/AR ARでユニティちゃんを表示したい <p>せっかくiPhone12Proを買ったので、LiDARを使って遊んでみたい。<br /> ということで調べたことをまとめていきます。</p> <p>できればかわいい女の子を自分の部屋に召喚したい...。</p> <p>2021/01/09追記<br /> →<a target="_blank" rel="nofollow noopener" href="https://itunes.apple.com/jp/app/id1547199178?mt=8">App Storeにリリースしました</a></p> <h3 id="ユニティちゃんAR表示のためにやったこと"><a href="#%E3%83%A6%E3%83%8B%E3%83%86%E3%82%A3%E3%81%A1%E3%82%83%E3%82%93AR%E8%A1%A8%E7%A4%BA%E3%81%AE%E3%81%9F%E3%82%81%E3%81%AB%E3%82%84%E3%81%A3%E3%81%9F%E3%81%93%E3%81%A8">ユニティちゃんAR表示のためにやったこと</a></h3> <ol> <li>ユニティちゃんをAssetStoreから入手</li> <li><a target="_blank" rel="nofollow noopener" href="https://github.com/Unity-Technologies/arfoundation-samples">ARFoundationのサンプル</a>のOcclusionMeshesのシーンを表示(このシーンを編集して実装を進める)</li> <li>タップした箇所にユニティちゃんを追加するスクリプトを「<a target="_blank" rel="nofollow noopener" href="https://jyuko.booth.pm/items/1560126">ARがわかる本</a>」4章の通り実装<br /> →実行サンプル動画</li> </ol> <blockquote class="twitter-tweet"><p lang="ja" dir="ltr">ARでユニティちゃんを出すとこまでできたボール飛ばす機能はARFoudationのサンプルそのままアニメーションとかできればもうちょいそれっぽくなりそう <a target="_blank" rel="nofollow noopener" href="https://t.co/9eVSSUA4wg">pic.twitter.com/9eVSSUA4wg</a></p>— カルロ (@carlvwxyz) <a target="_blank" rel="nofollow noopener" href="https://twitter.com/carlvwxyz/status/1338079268027265025?ref_src=twsrc%5Etfw">December 13, 2020</a></blockquote> <h3 id="ユニティちゃんの目線を自分に向けたい"><a href="#%E3%83%A6%E3%83%8B%E3%83%86%E3%82%A3%E3%81%A1%E3%82%83%E3%82%93%E3%81%AE%E7%9B%AE%E7%B7%9A%E3%82%92%E8%87%AA%E5%88%86%E3%81%AB%E5%90%91%E3%81%91%E3%81%9F%E3%81%84">ユニティちゃんの目線を自分に向けたい</a></h3> <p>このままではユニティちゃんが立っているだけなので、現実感に欠ける。<br /> こっちを向くように実装したい。</p> <p>下記スクリプトを設定したところ、想定した動作になった。<br /> <a target="_blank" rel="nofollow noopener" href="https://qiita.com/moe65/items/b40387f71514de0979df">Unityちゃんの見る方向をIKで変える方法 - Qiita</a></p> <p>ちゃんとこっちむいてる<br /> <a href="https://crieit.now.sh/upload_images/ecc06eaac64368e636d7f3ac2a60923c5fed8421036fb.jpg" target="_blank" rel="nofollow noopener"><img src="https://crieit.now.sh/upload_images/ecc06eaac64368e636d7f3ac2a60923c5fed8421036fb.jpg?mw=700" width="300"></a></p> <p>ただし後ろに回っても顔だけ変えようとするのが微妙<br /> 怖い...<br /> <a href="https://crieit.now.sh/upload_images/53be31eb673f488699ece846bad089bd5fed853b291aa.jpg" target="_blank" rel="nofollow noopener"><img src="https://crieit.now.sh/upload_images/53be31eb673f488699ece846bad089bd5fed853b291aa.jpg?mw=700" width="300"></a></p> <p>とりあえず、カメラがユニティちゃんの後ろに行ったらこちらを見ないように実装して回避</p> <h3 id="ユニティちゃんを動かしたい"><a href="#%E3%83%A6%E3%83%8B%E3%83%86%E3%82%A3%E3%81%A1%E3%82%83%E3%82%93%E3%82%92%E5%8B%95%E3%81%8B%E3%81%97%E3%81%9F%E3%81%84">ユニティちゃんを動かしたい</a></h3> <p>ボーッと立っててもアレなので、ランダムでユニティちゃんが動くように実装<br /> 下記のように基本の立ちポーズから、色々なモーションに移行するようなAnimatorを作成。<br /> あとはコードでランダムに移行させる。<br /> <a href="https://crieit.now.sh/upload_images/f60f7b53ac815dc84d027aea8eed800a5ffc14269aae2.png" target="_blank" rel="nofollow noopener"><img src="https://crieit.now.sh/upload_images/f60f7b53ac815dc84d027aea8eed800a5ffc14269aae2.png?mw=700" width="400"></a><br /> Conditionsに移行条件を設定しておく<br /> <a href="https://crieit.now.sh/upload_images/b99fe674b3f671004e46f44dfe20198e5ffc14b294a5d.png" target="_blank" rel="nofollow noopener"><img src="https://crieit.now.sh/upload_images/b99fe674b3f671004e46f44dfe20198e5ffc14b294a5d.png?mw=700" width="400"></a></p> <pre><code>public enum UnityChanMotion { Pose1, Wait3, Wait2, Wait1, Win0, Lose0, } private Animator _animator; private UnityChanMotion _motion; void Start() { _animator = GetComponent<Animator>(); _motion = UnityChanMotion.Pose1; _animator.SetInteger("AnimationPattern", (int)_motion); StartCoroutine("StartRandomMotion"); } private IEnumerator StartRandomMotion() { while (true) { if (_motion != UnityChanMotion.Pose1) { _motion = UnityChanMotion.Pose1; _animator.SetInteger("AnimationPattern", (int)_motion); yield return new WaitForSeconds(1.0f); } //一定の確率で何もしない if (UnityEngine.Random.Range(1, 100) < 90) { yield return new WaitForSeconds(1.0f); continue; } int max = (int)Enum.GetValues(typeof(UnityChanMotion)).Cast<UnityChanMotion>().Max(); int cnt = UnityEngine.Random.Range(1, max + 1); _motion = (UnityChanMotion)cnt; _animator.SetInteger("AnimationPattern", (int)_motion); // 指定した秒数だけ停止 yield return new WaitForSeconds(3.0f); } } </code></pre> <h3 id="AppStoreにリリース!"><a href="#AppStore%E3%81%AB%E3%83%AA%E3%83%AA%E3%83%BC%E3%82%B9%EF%BC%81">AppStoreにリリース!</a></h3> <p>このあたりを参考させて頂いて準備して提出!<br /> <a target="_blank" rel="nofollow noopener" href="https://i-app-tec.com/ios/app-upload.html">iOS アプリ申請のためXcodeでアップロードする</a><br /> <a target="_blank" rel="nofollow noopener" href="https://qiita.com/tanakaPH/items/727e3951a68a2b2a56e2">個人開発のUnityアプリをApp Storeに公開するまで - Qiita</a><br /> <a target="_blank" rel="nofollow noopener" href="https://qiita.com/Hiroki_Kawakami/items/94f012d20afdb692df20">個人開発者がAppStore用にプライバシーポリシーを書く - Qiita</a></p> <p>1度リジェクトを食らう<br /> 「During review, we found that your app includes TrueDepth APIs. However, we were not able to locate any features in your app that use TrueDepth APIs.」<br /> True Depth API使っているけど、アプリにその機能がないよ。いらないなら消してね。という感じ。<br /> これが若干詰まった。ARFoundationのサンプルプロジェクトでインストールされている、ARKit Face Trackingのパッケージがダメらしいのだが、removeしても消えない。なぜか自動的に再インストールされる<br /> <a target="_blank" rel="nofollow noopener" href="http://blog.lab7.biz/archives/25552116.html">【Unity】App Store申請時にTrueDepth APIでRejectされた際の対応(Unity AR Foundation使用)Part.1 : Lab 7 in Singapore</a></p> <p>なんとAssets¥XR¥Settingsフォルダ下にあるAR Kit SettingsのFace TrackingがTrueになっていると自動でインストールされるみたい。<br /> オフにして解決。<br /> この機能いる?<br /> <a href="https://crieit.now.sh/upload_images/aac37bd843be7fdd47204be3e0b2c80f5ffc17c6a1089.png" target="_blank" rel="nofollow noopener"><img src="https://crieit.now.sh/upload_images/aac37bd843be7fdd47204be3e0b2c80f5ffc17c6a1089.png?mw=700" alt="スクリーンショット 2021-01-11 18.17.53.png" /></a></p> <h3 id="参考URL等"><a href="#%E5%8F%82%E8%80%83URL%E7%AD%89">参考URL等</a></h3> <h4 id="メインで参考にさせて頂いたもの"><a href="#%E3%83%A1%E3%82%A4%E3%83%B3%E3%81%A7%E5%8F%82%E8%80%83%E3%81%AB%E3%81%95%E3%81%9B%E3%81%A6%E9%A0%82%E3%81%84%E3%81%9F%E3%82%82%E3%81%AE">メインで参考にさせて頂いたもの</a></h4> <p><a target="_blank" rel="nofollow noopener" href="https://www.jyuko49.com/entry/2020/03/30/222351">ARKit3.5およびAR Foundation4.0を試しました - じゅころぐAR</a><br /> →「ARがわかる本」を買ってARの基本をざっと試してみた</p> <p><a target="_blank" rel="nofollow noopener" href="https://github.com/Unity-Technologies/arfoundation-samples">Unity-Technologies/arfoundation-samples: Example content for Unity projects based on AR Foundation</a><br /> →ARFoundationのサンプル</p> <h4 id="以下情報集めに利用"><a href="#%E4%BB%A5%E4%B8%8B%E6%83%85%E5%A0%B1%E9%9B%86%E3%82%81%E3%81%AB%E5%88%A9%E7%94%A8">以下情報集めに利用</a></h4> <p><a target="_blank" rel="nofollow noopener" href="https://kaminashi-developer.hatenablog.jp/entry/2020/11/27/100409">iPhone 12 Pro(LiDAR) + ARFoundationで ARゲームことはじめ - カミナシ開発者ブログ</a><br /> →ARFoundationでLiDARの機能を使うやり方</p> <p><a target="_blank" rel="nofollow noopener" href="https://kaminashi-developer.hatenablog.jp/entry/2020/11/27/100409">iPhone 12 Pro(LiDAR) + ARFoundationで ARゲームことはじめ - カミナシ開発者ブログ</a></p> <hr /> <h3 id="以下自分用メモ"><a href="#%E4%BB%A5%E4%B8%8B%E8%87%AA%E5%88%86%E7%94%A8%E3%83%A1%E3%83%A2">以下自分用メモ</a></h3> <h4 id="新規プロジェクトでARFoundation4.1.1が動かない"><a href="#%E6%96%B0%E8%A6%8F%E3%83%97%E3%83%AD%E3%82%B8%E3%82%A7%E3%82%AF%E3%83%88%E3%81%A7ARFoundation4.1.1%E3%81%8C%E5%8B%95%E3%81%8B%E3%81%AA%E3%81%84">新規プロジェクトでARFoundation4.1.1が動かない</a></h4> <ol> <li>「ARがわかる本」2章を参考にプロジェクトを作成</li> <li>ARFoundation,ARKitを4.1.1にUpdate(LiDAR関連の機能使いたかった)<br /> →実行したら真っ暗なので、断念</li> </ol> <h4 id="ユニティちゃんが落ちる"><a href="#%E3%83%A6%E3%83%8B%E3%83%86%E3%82%A3%E3%81%A1%E3%82%83%E3%82%93%E3%81%8C%E8%90%BD%E3%81%A1%E3%82%8B">ユニティちゃんが落ちる</a></h4> <ul> <li>「ARがわかる本」7章の通り、Characterを動かすスクリプトを実装<br /> →落ちていくユニティちゃん(AR Plane Managerがないから?)</li> <li>それならばとy軸に落ちる処理を消す<br /> →明後日の方にいくユニティちゃん。(上にめり込んだ)</li> </ul> <p>CharacterControllerのチェックを外すと落ちなくなった。</p> <h4 id="ボールとユニティちゃんの当たり判定を追加"><a href="#%E3%83%9C%E3%83%BC%E3%83%AB%E3%81%A8%E3%83%A6%E3%83%8B%E3%83%86%E3%82%A3%E3%81%A1%E3%82%83%E3%82%93%E3%81%AE%E5%BD%93%E3%81%9F%E3%82%8A%E5%88%A4%E5%AE%9A%E3%82%92%E8%BF%BD%E5%8A%A0">ボールとユニティちゃんの当たり判定を追加</a></h4> <p>ユニティちゃんにCapsule Collidar,Rigidbodyを追加</p> <h4 id="ユニティちゃんをAnimationで動かしたい"><a href="#%E3%83%A6%E3%83%8B%E3%83%86%E3%82%A3%E3%81%A1%E3%82%83%E3%82%93%E3%82%92Animation%E3%81%A7%E5%8B%95%E3%81%8B%E3%81%97%E3%81%9F%E3%81%84">ユニティちゃんをAnimationで動かしたい</a></h4> <p><a target="_blank" rel="nofollow noopener" href="https://goodlucknetlife.com/unity-how-to-charactorcontroller/">CharacterControllerの使い方について【Unity】 | グッドラックネットライフ</a><br /> →CharacterControllerでキャラを動かせそう</p> <p><a target="_blank" rel="nofollow noopener" href="https://qiita.com/yamachan360/items/38b76a67ee0faef7e6b0">Unity初心者のためのアニメーションの基礎の基礎 - Qiita</a></p> <p><a target="_blank" rel="nofollow noopener" href="https://gametukurikata.com/animationanimator/changeanimation">Unityのアニメーションの切り替えシステムとスクリプト | Unityを使った3Dゲームの作り方(かめくめ)</a><br /> <a target="_blank" rel="nofollow noopener" href="https://unity-shoshinsha.biz/archives/471">【UnityC#講座】ユニティちゃんでもっとAnimator Controllerを上手く使いたい | Unityで魔物使い♀ゲームを作る</a></p> <h4 id="ユニティちゃんおいたら何かメニューが出る"><a href="#%E3%83%A6%E3%83%8B%E3%83%86%E3%82%A3%E3%81%A1%E3%82%83%E3%82%93%E3%81%8A%E3%81%84%E3%81%9F%E3%82%89%E4%BD%95%E3%81%8B%E3%83%A1%E3%83%8B%E3%83%A5%E3%83%BC%E3%81%8C%E5%87%BA%E3%82%8B">ユニティちゃんおいたら何かメニューが出る</a></h4> <p>unitychan_dynamicのFace Updateのチェックを外したら出なくなる</p> <h4 id="ユニティちゃんのAnimatorControllerについて"><a href="#%E3%83%A6%E3%83%8B%E3%83%86%E3%82%A3%E3%81%A1%E3%82%83%E3%82%93%E3%81%AEAnimatorController%E3%81%AB%E3%81%A4%E3%81%84%E3%81%A6">ユニティちゃんのAnimatorControllerについて</a></h4> <h5 id="UnityChanActionCheck"><a href="#UnityChanActionCheck">UnityChanActionCheck</a></h5> <p>デフォルトである<br /> 床設置しなければ落ちる<br /> キーを押すと設定されているアニメーションが順に実行される<br /> <a href="https://crieit.now.sh/upload_images/66bd711b7b1c7b7687ac6b199c5f097c5fed789c93266.png" target="_blank" rel="nofollow noopener"><img src="https://crieit.now.sh/upload_images/66bd711b7b1c7b7687ac6b199c5f097c5fed789c93266.png?mw=700" alt="image" /></a></p> <h5 id="UnityChanARPose"><a href="#UnityChanARPose">UnityChanARPose</a></h5> <p>キーを押すと設定されているアニメーションが順に実行される(ポーズ系)</p> <h3 id="ユニティちゃんを振り向かせたい"><a href="#%E3%83%A6%E3%83%8B%E3%83%86%E3%82%A3%E3%81%A1%E3%82%83%E3%82%93%E3%82%92%E6%8C%AF%E3%82%8A%E5%90%91%E3%81%8B%E3%81%9B%E3%81%9F%E3%81%84">ユニティちゃんを振り向かせたい</a></h3> <p>ユニティちゃんの背後に回った時に、後ろを振り返って欲しい<br /> 回転に関しては下記を参考に実装<br /> <a target="_blank" rel="nofollow noopener" href="https://tama-lab.net/2017/06/unity%E3%81%A7%E3%82%AA%E3%83%96%E3%82%B8%E3%82%A7%E3%82%AF%E3%83%88%E3%82%92%E5%9B%9E%E8%BB%A2%E3%81%95%E3%81%9B%E3%82%8B%E6%96%B9%E6%B3%95%E3%81%BE%E3%81%A8%E3%82%81/">Unityでオブジェクトを回転させる方法まとめ | tama-lab</a></p> <pre><code> //指定した方向にゆっくり回転する _target = Quaternion.Euler(0, 90f, 0); Quaternion nextAngle = Quaternion.RotateTowards(transform.rotation, _target, step); transform.rotation = nextAngle; //終了検知 if (Mathf.DeltaAngle(transform.rotation.x, _target.x) == 0 && Mathf.DeltaAngle(transform.rotation.y, _target.y) == 0 && Mathf.DeltaAngle(transform.rotation.z, _target.z) == 0) { _needRotate = false; //Debug.Log("Rotation end"); } </code></pre> <h3 id="他参考"><a href="#%E4%BB%96%E5%8F%82%E8%80%83">他参考</a></h3> <p><a target="_blank" rel="nofollow noopener" href="https://pengoya.net/unity/ui-fix/">【Unity】アスペクト比や解像度に合わせてUIの位置とサイズを固定する方法 | ぺんごや</a><br /> <a target="_blank" rel="nofollow noopener" href="https://tyfkda.github.io/blog/2016/11/13/unity-tap-ui.html">【Unity】タップされた位置がボタンの上だったら反応させない - Kludge Factory</a><br /> <a target="_blank" rel="nofollow noopener" href="https://gurutaka-log.com/unity-camera-flash-effect">【Unity】カメラのシャッターエフェクト(フラッシュ)を実装してみる | ぐるたかログ</a><br /> [Unity4.6 Unity UI (uGUI)のCanvas内で表示順(重なり)を調整する - タカシカンパニーブログ](http://takashicompany.hatenablog.com/entry/2014/09/02/234901</p> <h3 id="写真の保存"><a href="#%E5%86%99%E7%9C%9F%E3%81%AE%E4%BF%9D%E5%AD%98">写真の保存</a></h3> <p>info.plistに下記項目を追加し、アクセス権限要求文言を追加<br /> Privacy - Photo Library Usage Description</p> carl0967