https://www.youtube.com/watch?v=ARgf9Q8PLgI&list=PLXwWzhDeMm-zpSpCQOFxR9iyfMUqSLIoM&index=2

 

3D 프로젝트를 만들고 하이라키에 AR Session Orign, AR Session, AR Default Point Cloud, AR Default Plane을 추가하고 Main Camera를 지운다.

AR Session Origin에 AR Plane Manager, AR Point cloud Manager, AR Raycast Manager를 추가하자, 하이라키상의 AR Default Point Cloud, AR Default Plane을 끌어다 연결해준다

하이라키에 3D 객체를 스폰해준기 위한 EmptyObject를 CreateEmpty해주고 CarSpawner로 명명하고 ARPlaceOnPlane 스크립트를 추가

public class ARPlaceOnPlane : MonoBehaviour
{
    ARRaycastManager raycastManager;
    GameObject placeObject;

생성할 3D Object를 Import하던가 임시로 Cube를 생성해준다.

CarSpawner의 ARPlaceOnPlae스크립트에 public을 연결해주자 RayCastManager는 AR Session Origin의 컴포넌트이므로 그걸 끌어다 주자

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.XR.ARFoundation;
using UnityEngine.XR.ARSubsystems;

public class ARPlaceOnPlane : MonoBehaviour
{
     public ARRaycastManager raycastManager;
    public GameObject placeObject;
    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        
    }
    private void updateCenterObject()
    {
        Vector3 screenCenter = Camera.current.ViewportToScreenPoint(new Vector3(0.5f, 0.5f, 0));  
        List<ARRaycastHit> hits = new List<ARRaycastHit>();
        raycastManager.Raycast(screenCenter, hits, TrackableType.Planes);
        if (hits.Count > 0)
        {
            Pose placePose = hits[0].pose;
            placeObject.SetActive(true);
            placeObject.transform.SetPositionAndRotation(placePose.position, placePose.rotation);
        }
        else
        {
            placeObject.SetActive(false);
        }
    }
}

+ Recent posts