ARFoundationを使って赤い球体の間に違うオブジェクトを置きたいと考えています。
真ん中の白丸は赤い球体をどこに置くか決めるエイムみたいなものです。
一つ目のソースコードで赤い球体の中心点を求め、Instantiate(centerObj);で表示させようとしているのですが、実機テストをすると処理されません。ご教示していただけると幸いです。
C#
1 public void CalculateDistance() 2 { 3 // list_toggle_[0]は赤い球体のリストでcenterで中心点の算出 4 5 Vector3 pos1 = list_toggle_[0].transform.position; 6 Vector3 pos2 = list_toggle_[1].transform.position; 7 Vector3 center = (pos1 + pos2) / 2; 8 9 centerObj.transform.position = center; 10 11 Instantiate(centerObj); 12 }
C#
1using System; 2using System.Collections; 3using System.Collections.Generic; 4using UnityEngine; 5using UnityEngine.XR.ARFoundation; 6using UnityEngine.XR.ARSubsystems; 7 8public class ARPoint : MonoBehaviour 9{ 10 public GameObject Pointobj; 11 [SerializeField] 12 private ARSessionOrigin arS; 13 14 [SerializeField] 15 private ARRaycastManager arRaycastManager; 16 17 private Pose Points; 18 private bool placeV = false; 19 public GameObject obj; 20 public GameObject centerObj; 21 TrackableType trackable = TrackableType.Planes; 22 23 List<GameObject> list_toggle_ = new List<GameObject>(); 24 25 private void Update() 26 { 27 UpdatePlacementPose(); 28 UpdatePlacementIndicator(); 29 30 if (placeV && Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began) 31 { 32 PlaceObject(); 33 } 34 } 35 36 private void PlaceObject() 37 { 38 GameObject toggle_instantiate = Instantiate(obj, Points.position, Points.rotation) as GameObject; 39 list_toggle_.Add(toggle_instantiate); 40 } 41 42 public void CalculateDistance() 43 { 44 Vector3 pos1 = list_toggle_[0].transform.position; 45 Vector3 pos2 = list_toggle_[1].transform.position; 46 47 Vector3 center = (pos1 + pos2) / 2; 48 49 centerObj.transform.position = center; 50 51 Instantiate(centerObj); 52 } 53 54 private void UpdatePlacementIndicator() 55 { 56 57 if (placeV) 58 { 59 Pointobj.SetActive(true); 60 61 Pointobj.transform.SetPositionAndRotation(Points.position, Points.rotation); 62 } 63 else { 64 Pointobj.SetActive(false); 65 66 } 67 68 } 69 70 private void UpdatePlacementPose() 71 { 72 var screenCenter = arS.camera.ViewportToScreenPoint(new Vector3(0.5f, 0.5f, 0.5f)); 73 var hits = new List<ARRaycastHit>(); 74 75 arRaycastManager.Raycast(screenCenter,hits,trackable); 76 77 78 placeV = hits.Count > 0; 79 80 if (placeV) 81 { 82 83 Points = hits[0].pose; 84 85 var cameraForward = arS.camera.transform.forward; 86 var cameraBearing = new Vector3(cameraForward.x, 0, cameraForward.z).normalized; 87 88 Points.rotation = Quaternion.LookRotation(cameraBearing); 89 90 } 91 } 92 93 public void Reset() 94 { 95 for (int i = 0; i < list_toggle_.Count; i++) 96 { 97 Destroy(list_toggle_[i]); 98 } 99 //リスト自体をキレイにする 100 list_toggle_.Clear(); 101 } 102}
あなたの回答
tips
プレビュー