AR 콘텐츠 기초

[AR Foundation] Car(지면 생성 및 지면 클릭 시 차 생성)

다모아 2023. 11. 1. 13:19

https://docs.unity3d.com/Packages/com.unity.xr.arfoundation@5.0/api/UnityEngine.XR.ARFoundation.ARPlaneManager.html

 

Class ARPlaneManager | AR Foundation | 5.0.7

Class ARPlaneManager A manager for ARPlanes. Creates, updates, and removes GameObjects in response to detected surfaces in the physical environment. Inherited Members UnityEngine.MonoBehaviour.IsInvoking() UnityEngine.MonoBehaviour.CancelInvoke() UnityEngi

docs.unity3d.com

 

https://docs.unity3d.com/Packages/com.unity.xr.arfoundation@5.0/manual/features/features.html

 

Features | AR Foundation | 5.0.7

Features AR Foundation supports the following features: Feature Description Session Enable, disable, and configure AR on the target platform. Device tracking Track the device's position and rotation in physical space. Camera Render images from device camer

docs.unity3d.com

 

SRDebugger - 안드로이드에서 로그를 볼 수 있음


XR Origin에 Camera가 있어서 Main Camera는 삭제

 

XR Origin에 AR Plane Manager 추가하고 Detection Mode를 Horizontal로 변경

 

DetectedPlane이라는 빈 오브젝트 추가하고 Mesh Renderer, Filter, Collider, AR Plane Mesh Visualizer[이거 추가하면 AR Plane 자동으로 추가] 추가

 

Material 추가, Rendering Mode - Transparent [반투명으로 보이게] , Color.A 값 166으로 수정

 

 

 

카메라 안 켜질 때 IL2CPP랑 ARM64 체크

 

Indicator.png

https://github.com/araxrlab/xrlifeunity/tree/master/2%EC%9E%A5%20%EC%82%AC%EC%9A%A9%20%EC%97%90%EC%85%8B

 

M_Indicator의 Albedo에 드래그 앤 드롭

 

Quad를 만들어주고 이름을 Indicator로 변경, Materials에 M_Indicator 넣기

 

CarManager 스크립트 추가, XR Origin에 추가

 

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

public class CarManager : MonoBehaviour
{
    [SerializeField] private GameObject indicatorGo;

    [SerializeField] private ARRaycastManager arManager;

    [SerializeField] private GameObject carPrefab;

    private GameObject carGo;

    // Start is called before the first frame update
    void Start()
    {
        this.indicatorGo.SetActive(false);
    }

    // Update is called once per frame
    void Update()
    {
        this.DetectGround();

        if(this.indicatorGo.activeInHierarchy && Input.touchCount > 0)
        {
            Touch touch = Input.GetTouch(0);
            if(touch.phase == TouchPhase.Began)
            {
                if(this.carGo == null)
                {
                    this.carGo = Instantiate(this.carPrefab, this.indicatorGo.transform.position, this.indicatorGo.transform.rotation);
                }
                else
                {
                    this.carGo.transform.SetPositionAndRotation(indicatorGo.transform.position, indicatorGo.transform.rotation);
                }
                

            }
        }
    }

    private void DetectGround()
    {
        Vector2 screenSize = new Vector2(Screen.width * 0.5f, Screen.height * 0.5f); // 스크린의 중앙
        List<ARRaycastHit> hitInfos = new List<ARRaycastHit>();
        if(this.arManager.Raycast(screenSize, hitInfos, TrackableType.Planes))
        {
            this.indicatorGo.SetActive(true);
            this.indicatorGo.transform.position = hitInfos[0].pose.position; // world
            this.indicatorGo.transform.rotation = hitInfos[0].pose.rotation;

            this.indicatorGo.transform.position += this.indicatorGo.transform.up * 0.1f;
        }
        else
        {
            this.indicatorGo.SetActive(false);
        }
    }
}

 

https://assetstore.unity.com/packages/3d/vehicles/land/arcade-free-racing-car-161085

 

ARCADE: FREE Racing Car | 3D 지상 | Unity Asset Store

Elevate your workflow with the ARCADE: FREE Racing Car asset from Mena. Find this & other 지상 options on the Unity Asset Store.

assetstore.unity.com