ARPlaceOnPlane.cs 1.41 KB
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.XR.ARFoundation;
using UnityEngine.XR.ARSubsystems;

public class ARPlaceOnPlane : MonoBehaviour
{

    public ARRaycastManager arRaycaster;
    public GameObject[] placeObject;
    
    GameObject spawnObject;

    public static bool isSetted = false;

    void Update()
    {
        
    }
    private void FixedUpdate()
    {//UpdateCenterObject();
        if (!isSetted)
        {
            PlaceObjectByTouch();
        }

    }
    private void PlaceObjectByTouch() {
        if (Input.touchCount > 0) {
            Touch touch = Input.GetTouch(0); //터치값 얻어오기

            List<ARRaycastHit> hits = new List<ARRaycastHit>();
            if (arRaycaster.Raycast(touch.position, hits, TrackableType.Planes)) {
                Pose hitPose = hits[0].pose;

                if (!spawnObject) // 아직 오브젝트 미생성됐으면
                {
                    int ran = Random.Range(0, 3);
                    
                    spawnObject = Instantiate(placeObject[ran], hitPose.position, hitPose.rotation); // 생성하고
                    isSetted = true; // update문에서 바닥탐지/생성함수를 정지함
                }
                else {
                    spawnObject.transform.position = hitPose.position;
                    spawnObject.transform.rotation = hitPose.rotation;
                }
            }
        }
    }
}