前提・実現したいこと
・Unity AR Hit test Exampleの中のCollision LayerにARKitPlaneを追加したい
・UnityのARKitを使ってUnityちゃんをカメラ越しの平面上に出現させたい。
発生している問題・エラーメッセージ
Unityちゃんを出現させること自体はできているんですが、落ちていってどこかへ消えてしまう。
おそらくUnity AR Hit test Exampleの中のCollision LayerのなかにARKitPlane(カメラ越しで認識された平面を床として扱う?)がないからだと思います。
該当のソースコード
Scriptと書いてあるので、該当するcsファイルを確認したところ
C#
1using System; 2using System.Collections.Generic; 3 4using UnityEngine.EventSystems; 5 6namespace UnityEngine.XR.iOS 7{ 8 public class UnityARHitTestExample : MonoBehaviour 9 { 10 public Transform m_HitTransform; 11 public float maxRayDistance = 30.0f; 12 public LayerMask collisionLayer = 1 << 10; //ARKitPlane layer ←多分これ 13 14 15 bool HitTestWithResultType (ARPoint point, ARHitTestResultType resultTypes) 16 { 17 List<ARHitTestResult> hitResults = UnityARSessionNativeInterface.GetARSessionNativeInterface ().HitTest (point, resultTypes); 18 if (hitResults.Count > 0) { 19 foreach (var hitResult in hitResults) { 20 Debug.Log ("Got hit!"); 21 m_HitTransform.position = UnityARMatrixOps.GetPosition (hitResult.worldTransform); 22 m_HitTransform.rotation = UnityARMatrixOps.GetRotation (hitResult.worldTransform); 23 Debug.Log (string.Format ("x:{0:0.######} y:{1:0.######} z:{2:0.######}", m_HitTransform.position.x, m_HitTransform.position.y, m_HitTransform.position.z)); 24 return true; 25 } 26 } 27 return false; 28 } 29 30 31 32 33 // Update is called once per frame 34 void Update() 35 { 36#if UNITY_EDITOR //we will only use this script on the editor side, though there is nothing that would prevent it from working on device 37 if (Input.GetMouseButtonDown(0)) 38 { 39 Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); 40 RaycastHit hit; 41 42 //we'll try to hit one of the plane collider gameobjects that were generated by the plugin 43 //effectively similar to calling HitTest with ARHitTestResultType.ARHitTestResultTypeExistingPlaneUsingExtent 44 if (Physics.Raycast(ray, out hit, maxRayDistance, collisionLayer)) 45 { 46 //we're going to get the position from the contact point 47 m_HitTransform.position = hit.point; 48 Debug.Log(string.Format("x:{0:0.######} y:{1:0.######} z:{2:0.######}", m_HitTransform.position.x, m_HitTransform.position.y, m_HitTransform.position.z)); 49 50 //and the rotation from the transform of the plane collider 51 m_HitTransform.rotation = hit.transform.rotation; 52 } 53 } 54 #else 55 if (Input.touchCount > 0 && m_HitTransform != null) && IsPointerOverUIObject() 56 { 57 var touch = Input.GetTouch(0); 58 if (touch.phase == TouchPhase.Began || touch.phase == TouchPhase.Moved) 59 { 60 var screenPosition = Camera.main.ScreenToViewportPoint(touch.position); 61 ARPoint point = new ARPoint { 62 x = screenPosition.x, 63 y = screenPosition.y 64 }; 65 66 // prioritize reults types 67 ARHitTestResultType[] resultTypes = { 68 //ARHitTestResultType.ARHitTestResultTypeExistingPlaneUsingGeometry, 69 ARHitTestResultType.ARHitTestResultTypeExistingPlaneUsingExtent, 70 // if you want to use infinite planes use this: 71 //ARHitTestResultType.ARHitTestResultTypeExistingPlane, 72 //ARHitTestResultType.ARHitTestResultTypeEstimatedHorizontalPlane, 73 //ARHitTestResultType.ARHitTestResultTypeEstimatedVerticalPlane, 74 //ARHitTestResultType.ARHitTestResultTypeFeaturePoint 75 }; 76 77 foreach (ARHitTestResultType resultType in resultTypes) 78 { 79 if (HitTestWithResultType (point, resultType)) 80 { 81 return; 82 } 83 } 84 } 85 } 86#endif 87 88 } 89 90 91 } 92} 93 94
一応宣言自体はされているみたいです。
あくまで初心者の推測なので見当違いかも知れませんがとりあえず落ちて行っちゃうのをどうにかしたいです。
どうかお助けください。
試したこと
Unityのバージョンが少し古かったので最新のものにしましたが結果は変わりませんでした。
あとARKit自体がAsset Storeから消えてるっぽかったので、いろんなサイト探し回ってARKitのPluginを何個か試しました。
補足情報(FW/ツールのバージョンなど)
参考書:ARKitとUnityで始めるARアプリ開発 https://www.amazon.co.jp/dp/4798054364/ref=cm_sw_em_r_mt_dp_U_aoliCb2463YA2
Macbook pro macOS Mojava10.14.2
Xcode10.1
Unity: Version 2018.3.0f2 Personal
ARkit: https://bitbucket.org/Unity-Technologies/unity-arkit-plugin/get/arkit2.0_beta.zip
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。