インジケータって?
こういうの
ARで任意の場所にオブジェクトを置きたい時のターゲットインジケータってわけです。
作り方
ARFoundationを使うので、予めパッケージに追加しといて下さい。
インジケータのプレハブを用意
インジケータオブジェクトを作成
ヒエラルキーで空のオブジェクトを作成します(適当にIndicatorとか名前つける)
空のオブジェクト配下にPlaneを作成します。
Planeにインジケータとして表示したいテクスチャをドラッグ&ドロップで貼り付けます。
必要なコンポーネントを追加
AR Plane ManagertとAR Raycast Managerを追加します。
スクリプト作成
解説めんどくさいから全部貼ります。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.XR.ARFoundation;
using UnityEngine.XR.ARSubsystems;
public class PlacementIndicator : MonoBehaviour
{
private ARRaycastManager rayManager;
private GameObject visual;
void Start ()
{
// コンポーネントを入手する
rayManager = FindObjectOfType<ARRaycastManager>();
visual = transform.GetChild(0).gameObject;
// インジケーターを非表示にする
visual.SetActive(false);
}
void Update ()
{
// 画面の中央からレイキャストを発射します
List<ARRaycastHit> hits = new List<ARRaycastHit>();
rayManager.Raycast(new Vector2(Screen.width / 2, Screen.height / 2), hits, TrackableType.Planes);
// AR平面に当たった場合は、位置と回転を更新します
if(hits.Count > 0)
{
transform.position = hits[0].pose.position;
transform.rotation = hits[0].pose.rotation;
// ビジュアルが無効になっている場合は有効にする
if(!visual.activeInHierarchy)
visual.SetActive(true);
}
}
}
スクリプトをIndicatorオブジェクトにアタッチ
さっき空のオブジェクトから作ったIndicatorオブジェクトに作ったスクリプトをドラッグ&ドロップしてアタッチします。