[ Vuforia로 VR 만들기 기본 설정 ]
Vuforia를 이용한 VR 환경 설정
Play Settings ..
Other Settings
Identification : Bundle Identifer : 알아서 넣을 것.
Minimum API Level : 최소 Android 4.1 이상으로 하자.
XR Settings - Vuforia 사용 체크
Virtual Reality Supported 체크 확인
Virtual Reality SDKs : Vuforia 선택
Vuforia Augmented Reality 체크 확인
Vuforia 카메라 설치
GameObject >> Vuforia >> AR Camera
Vuforia에 필요한 몇 가지 애셋을 import할 건지를 물어 보면 ... import
AR Camera Inspector
Camera Component >> Clear Flags : Skybox로 선택
Vuforia Behaviour Component
World Center Mode : DEVICE_TRACKING
DEVICE_TRACKING이 없을 경우 DEVICE로 선택
Digital Eyewear : Device Type : Phone+Viewer
Video Background : Overlflow geometry : NONE
Device Tracker : Track Device Pose : Check
Tracking mode : ROTATIONAL
GameObject 띄우기
GameObject >> 3D Object >> Cube
이미지의 상대적인 위치 및 크기를 생각하여 크기 및 위치 조정
VR Test
Play Settings ... >> Bulid
Apk 파일 설치
[ Vuforia로 VR 만들기 ]
모델하우스 Asset 다운 받기
Window >> Asset Store
검색 : Simple Home Stuff
DownLoad
Import
Project view : Assets : HomeStuff >> Demo(Scene) 더블 클릭
모델하우스 복사해서 사용하기
GameObject >> Create Empty
Name : ModelRoom
Hierarchy View 내의 모델하우스를 이루는 데이타들을 드래그 >> (to) ModelRoom
ModelRoom 프리팹화 하기
Project view : Assets 에 폴더 만들기 (Name : ModelRoom)
Hierachy View : ModelRoom 드래그 >> Project View : ModelRoom 폴더
모델하우스 불러오기
이전 씬(Scene)으로 돌아오기
Project View : ModelRoom Prefab 드래그 >> (to) Hierarchy View
ModelRoom : Transform Component : Position (0, 0, 0)
캐릭터 생성하기
※ 게임 실행시 ARCamera가 이동불가한 문제점을 해결하기 위함
GameObject >> 3D Object >> Capsule
Capsule 의 이름 변경 : Head
Transform : Position : (-10, 2.5, -5)
Hierarchy View ARCamera 드래그 >> (to) Head or Player 자식 오브젝트로 위치
ARCamera Transform Component 초기화
플레이어의 시점 생성하기
GameObject >> UI >> Image
EventSystem & Canvas 라는 게임 오브젝트 생성
Canvas를 AR Camera의 자식 오브젝트로 위치
Canvas Object Setting
Canvas Transform Component
Canvas Component : Render Mode : World Space
Screen Space - Overlay 모드는 화면 크기와 해상도가 변경되었을 때 스크린에 일치하도록 자동으로 크기를 변경시켜주는 모드이다.
Screen Space - Camera 모드는 Overlay와 비슷하지만 지정된 거리의 Camera 앞에 배치하기 때문에 카메라의 설정이 UI 모양에 영향을 끼치는 모드이다.
World Space 모드는 Canvas를 씬 내의 오브젝트처럼 작동하는 모드 (가상현실 속의 메뉴로 주로 사용된다)
Image Object Setting
Image Transform Component
Image (Script) Component : Source Image : Knob
눈 앞의 원을 기준으로 레이케스트(Raycast) 생성하기
Assets >> Create >> C# Script : Raycast
Edit C# Script 드래그 >> (to) ARCamera
Play Button
레이케스트가 잘 나가는지 확인
물체를 바라보면 물체를 향해서 이동하기
Hierarchy View : ModelHome : Refrigerator 선택
Add Component >> Box Collider (크기 자동맞춤)
Play Button
Colsole View : Refrigerator라는 문구가 뜨는지 확인
GameObject >> Create Empty : Point_01
Transform Component : Position : (-10, 2.5, 8)
ModelHome : Refrigerator : Tag : Add Tag
Tags : + 버튼 : refrigerator 생성
Refrigerator : Tag : refrigerator 변경
Edit Raycast Script
※ 냉장고를 보았을 때 Point_01으로 이동
ARCamera : Raycast Component : Head >> Head Object
ARCamera : Raycast Component : Pos1 >> Point_01 Object
Play Button
냉장고쪽으로 잘 이동하는지 확인
방입구에 문 생성하기
GameObject >> Create Empty : Door
GameObject >> 3D Object >> Cube
큐브에 태그 달아주기
Cube : Tag : door 생성 및 변경
방입구에 목적지 생성하기
GameObject >> Create Empty : Point02
Transform Component : Position : (2.5, 2.5, -7.5)
방입구로 이동하는 스크립트(Script) 작성하기
Edit Raycast Script
ARCamera : Raycast (Script) Component : Pos2 >> Point_02 Object
ARCamera : Raycast (Script) Component : Door >> Door Object
Play Button
냉장고쪽, 문쪽으로 이동해보며 문이 잘 열리는지 확인
using UnityEngine;
public class Raycast : MonoBehaviour
{
// Update is called once per frame
private void Update()
{
RaycastHit hit;
Vector3 forward = transform.TransformDirection(Vector3.forward) * 1000;
Debug.DrawRay(transform.position, forward, Color.green);
if(Physics.Raycast(transform.position, forward, out hit))
{
Debug.Log(hit.transform.name);
}
}
}
using System.Collections;
using UnityEngine;
public class Raycast : MonoBehaviour
{
public GameObject head;
public GameObject pos1;
// Update is called once per frame
private void Update()
{
RaycastHit hit;
Vector3 forward = transform.TransformDirection(Vector3.forward) * 1000;
Debug.DrawRay(transform.position, forward, Color.green);
if(Physics.Raycast(transform.position, forward, out hit))
{
if(hit.collider.tag == "refrigerator")
{
StartCoroutine(moveCo());
}
Debug.Log(hit.collider.tag);
}
}
IEnumerator moveCo()
{
while(head.transform.position != pos1.transform.position)
{
head.transform.position = Vector3.MoveTowards(head.transform.position, pos1.transform.position, Time.deltaTime * 0.05f);
yield return null;
}
}
}
using System.Collections;
using UnityEngine;
public class Raycast : MonoBehaviour
{
public GameObject head;
public GameObject pos1;
public GameObject pos2;
public GameObject door;
private bool isDoor;
// Update is called once per frame
private void Update()
{
RaycastHit hit;
Vector3 forward = transform.TransformDirection(Vector3.forward) * 1000;
Debug.DrawRay(transform.position, forward, Color.green);
if(Physics.Raycast(transform.position, forward, out hit))
{
if(hit.collider.tag == "refrigerator")
{
isDoor = false;
StartCoroutine(moveCo(pos1));
}
else if(hit.collider.tag == "door")
{
isDoor = true;
StartCoroutine(moveCo(pos2));
}
}
}
IEnumerator moveCo(GameObject pos)
{
while(head.transform.position != pos.transform.position)
{
head.transform.position = Vector3.MoveTowards(head.transform.position, pos.transform.position, Time.deltaTime * 0.05f);
if(isDoor)
{
door.transform.rotation = Quaternion.Slerp(door.transform.rotation, Quaternion.Euler(0, 120, 0), Time.deltaTime);
}
yield return null;
}
}
}
[ 참고 ] 시선의 방향에 따라 움직이도록 하기
기존의 Raycast(Script)를 비활성화 시켜야 함
Hierarchy View : Head : ARCamera >> Raycast(Script 컴포넌트)의 왼쪽 체크를 Un Check
Script 생성 (Project View : Assets : Scripts 폴더 내에서 오른쪽 마우스 클릭 >> Create >> C#)
Name : LookMoveTo
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class LookMoveTo : MonoBehaviour {
public GameObject ground;
public Transform camera;
public float Speed = 0.01f;
void Update () {
Ray ray;
RaycastHit[] hits;
GameObject hitObject;
Debug.DrawRay(camera.position, camera.rotation * Vector3.forward * 100.0f,Color.green);
ray = new Ray(camera.position, camera.rotation * Vector3.forward * 100.0f);
hits = Physics.RaycastAll(ray);
for (int i = 0; i< hits.Length; i++)
{
RaycastHit hit = hits[i];
hitObject = hit.collider.gameObject;
if(hitObject == ground)
{
Debug.Log("Hit (x,y,z): " + hit.point.ToString("F2"));
Vector3 MovePoint = Vector3.Lerp (transform.position, hit.point, Time.deltaTime*Speed);
MovePoint.y = transform.position.y;
transform.position = MovePoint;
}
}
}
}
LookMoveTo(Script) 드래그 >> (to) Head
바닥 오브젝트 이름 변경
Hierarchy View : ModelHouse : Plane >> Name 변경 : ground
Head Inspector : LookMoveTo Component
Hierarchy View : ModelHouse : ground 드래그 >> (to) Ground (in LookMoveTo)
Hierarchy View : Head : ARCamera 드래그 >> (to) Camera (in LookMoveTo)
Play Test
바닥 면을 보는 곳으로 이동하게 됨 (속도를 높이고자 하는 경우 Speed의 값을 높여 주면 됨)