ARFoundation으로 ARCore 사용하기 - 바닥면에 객체 생성
AR Raycast를 사용해서 감지된 바닥면에 오브젝트를 생성해봅시다.
Hierarchy에 미리 생성해 놓은 AR Sessiohn Origin 객체에 AR RaycastManager 컴포넌트를 추가합니다.
Hierarchy에 Spawner라는 이름으로 빈 게임오브젝트를 생성합니다.
그리고 스크립트를 새로 생성해 줍니다. 이름은 PlaceOnPlane으로 생성하겠습니다.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.XR.ARFoundation;
using UnityEngine.XR.ARSubsystems;
public class PlaceOnPlane : MonoBehaviour
{
public ARRaycastManager aRRaycastManager;
public GameObject spawnPrefab;
private List<ARRaycastHit> hits = new List<ARRaycastHit>();
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
if (Input.touchCount == 0)
{
return;
}
Touch touch = Input.GetTouch(0);
if (touch.phase != TouchPhase.Began)
{
return;
}
if (aRRaycastManager.Raycast(touch.position, hits, TrackableType.Planes))
{
Pose hitPose = hits[0].pose;
Instantiate(spawnPrefab, hitPose.position, hitPose.rotation);
}
}
}
이 스크립트를 Spawner 객체의 컴포넌트로 등록해 줍니다.
그리고 AR RaycastManager와 Prefab을 등록해 줍니다. AR RaycastManager는 아까 AR Session Origin에 생성해 주었습니다.
이제 APK로 빌드 후, 모바일에서 테스트를 해봅시다.
'Unity > ARCore' 카테고리의 다른 글
ARFoundation으로 ARCore 사용하기 - 객체를 중심으로 세상을 이동 (0) | 2020.10.27 |
---|---|
ARFoundation으로 ARCore 사용하기 - 세션 상태 (0) | 2020.10.27 |
ARFoundation으로 ARCore 사용하기 - PointCloud, Plane Manager (0) | 2020.10.27 |
ARFoundation으로 ARCore 사용하기 - 초기셋팅 (0) | 2020.10.27 |
Android 용 ARCore extensions을위한 빠른 시작 1 (0) | 2020.10.22 |