728x90
Unity Asset Store에 가서 Asset 구매 후 Unity에서 열기
Pachage Manager - import
Assets - 폴더 - Prefabs - 선택 후 Hierarchy로 드래그 앤 드롭
크기, 방향 조절 후 Assets - Prefabs 폴더로 드래그 앤 드롭
Scripts - CarManager 소스 열기
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.XR.ARFoundation;
public class CarManager : MonoBehaviour
{
public GameObject indicator;
public GameObject myCar;
ARRaycastManager arManager;
GameObject placedObject;
// Start is called before the first frame update
void Start()
{
// 시작하자마자 인디케이터 비활성화
indicator.SetActive(false);
// AR Raycast Manager 컴포넌트를 가져옴
arManager = GetComponent<ARRaycastManager>();
}
// Update is called once per frame
void Update()
{
DetectGound();
// 자동차 모델링 생성
// 인디케이터 활성화 되어 있으면서 화면 터치가 있다면
if(indicator.activeInHierarchy && Input.touchCount > 0)
{
Touch touch = Input.GetTouch(0);
if(touch.phase == TouchPhase.Began)
{
if(placedObject == null)
{
placedObject = Instantiate(myCar, indicator.transform.position, indicator.transform.rotation);
}
else
{
placedObject.transform.SetPositionAndRotation(indicator.transform.position, indicator.transform.rotation);
}
}
}
}
// 바닥 감지 및 indicator 출력 함수
void DetectGound()
{
Vector2 screenSize = new Vector2(Screen.width * 0.5f, Screen.height * 0.5f);
List<ARRaycastHit> hitInfos = new List<ARRaycastHit>();
// ray를 이용해 바닥 감지
if(arManager.Raycast(screenSize, hitInfos, UnityEngine.XR.ARSubsystems.TrackableType.Planes))
{
// 인디케이터 활성화
indicator.SetActive(true);
// 인디케이터의 위치와 회전 값을 레이가 닿은 지점에 일치
indicator.transform.position = hitInfos[0].pose.position;
indicator.transform.rotation = hitInfos[0].pose.rotation;
// 인디케이터 위치를 위로 0.1미터 올림
indicator.transform.position += indicator.transform.up * 0.1f;
}
else
{
indicator.SetActive(false);
}
}
}
728x90
'프로그래밍' 카테고리의 다른 글
[MovaXterm] 서버 연결하기 (0) | 2022.10.26 |
---|---|
[Anaconda] Solving environment: failed with initial frozen solve. Retrying with flexible solve 오류 (0) | 2022.10.25 |
[Unity] AR_Foundation: indicator 만들기 (0) | 2022.10.19 |
[Unity] AR_Foundation: AR Plane Manager 평면 생성 (0) | 2022.10.19 |
[Unity] AR_Foundation Android build setting (0) | 2022.10.19 |