VR에서 UI를 띄우려면 사용자가 바라보는 방향에 맞춰서 띄워줘야 한다
만약 UI를 x, y, z 좌표를 이용해서 설정한다면 사용자가 고개를 돌리거나 이동하면 보이지 않기 때문이다
체력바를 UI로 띄워주는 실습
# CameraMove.cs
키 입력에 따라 카메라 이동
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraMove : MonoBehaviour
{
public float speed = 5f;
public float rotateSpeed = 1f;
Vector3 gap = Vector3.zero;
void Start()
{
}
void Update()
{
// 카메라를 월드에서 키보드로 이동
if (Input.GetKey(KeyCode.W))
{
transform.Translate(new Vector3(0, 0, 1) * speed * Time.deltaTime); //전진
}
if (Input.GetKey(KeyCode.S))
{
transform.Translate(new Vector3(0, 0, -1) * speed * Time.deltaTime); //후진
}
if (Input.GetKey(KeyCode.A))
{
transform.Translate(new Vector3(-1, 0, 0) * speed * Time.deltaTime); //상
}
if (Input.GetKey(KeyCode.D))
{
transform.Translate(new Vector3(1, 0, 0) * speed * Time.deltaTime); //하
}
if (Input.GetKey(KeyCode.Q))
{
transform.Translate(new Vector3(0, 1, 0) * speed * Time.deltaTime); //좌
}
if (Input.GetKey(KeyCode.E))
{
transform.Translate(new Vector3(0, -1, 0) * speed * Time.deltaTime); //우
}
// 드래그(왼쪽 클릭하여 이동)시 카메라 회전
if (Input.GetMouseButtonDown(0))
{
gap.y = Input.GetAxis("Mouse X") * rotateSpeed;
gap.x = Input.GetAxis("Mouse Y") * rotateSpeed;
gap.x = Mathf.Clamp(gap.x, -50f, 50f); // 위 아래 각도 제한을 위 아래로 50도 제한
transform.localEulerAngles = gap;
}
}
}
# HPGauge.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class HPGauge : MonoBehaviour
{
public Slider slider;
public Image gauge;
float hp = 1f;
float limitTime = 5f;
float nowTime = 0;
void Start()
{
gauge.fillAmount = hp;
//slider.value = hp;
}
void Update()
{
if (nowTime < limitTime)
{
nowTime = Time.deltaTime;
//slider.value = nowTime / limitTime;
gauge.fillAmount = nowTime / limitTime; // 게이지 늘어나는 방법
//gauge.fillAmount = 1 - nowTime / limitTime; //게이지 줄어드는 방법
}
//if (Input.GetKeyDown(KeyCode.Q))
//{
// hp -= 0.2f;
// if (hp < 0) hp = 0;
// gauge.fillAmount = hp;
//}
}
}
# ObjectBillboard.cs
풀과 같은 이미지를 2d 이미지로 설정해두고 사용자가 바라볼때 회전하도록 함
그러면 풀이 2d 이미지라도 사용자는 3d 이미지처럼 볼 수 있음
오일러 사용해서 회전 정보 설정
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ObjectBillboard : MonoBehaviour
{
public Camera camera;
void Update()
{
// 카메라가 향하고 있는 방향으로 회전
transform.forward = camera.transform.forward;
// 회전 정보를 가져와 x 축 회전을 제거
Vector3 dir = transform.rotation.eulerAngles;
dir.x = 0;
// 오일러 회전정보를 쿼터니언으로 변환하여 적용
transform.rotation = Quaternion.Euler(dir);
}
// 풀과 같은 오브젝트는 위에서 봤을 때 카메라를 바라보게 하면 이상해지므로 x축회전을 제거시켜서 위에서 봤을 땐 회전하지 않도록 한다
}
# UIBillboard
UI를 사용자가 바라보는 곳을 기준으로 설정하도록 함
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class UIBillboard : MonoBehaviour
{
public Camera camera;
void Start()
{
}
void Update()
{
transform.forward = camera.transform.forward; // 내가 바라보는 정면을 카메라도 바라보게
}
}
수업 내용 정리
'etc. > 🕹️unity' 카테고리의 다른 글
VR 개인 프로젝트 (0) | 2022.06.11 |
---|---|
VR 실습 (0) | 2022.06.11 |
2D Shooting (0) | 2022.06.11 |
3D FPS (0) | 2022.06.11 |
unity shader, effect, light & 지형 만들기 (0) | 2022.06.11 |
댓글