Unity
unity 유니티 ] 카메라 시선방향으로 이동하기
언제나 변함없이
2019. 5. 26. 11:11
반응형
MoveTowards.cs : 적용시킨 객체를 카메라 방향으로 나아갑니다.(위/ 아래로는 움직이지 않습니다.)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MoveTowards : MonoBehaviour
{
/* 시점 방향으로 이동하는 스크립트입니다. */
public Camera cam; //메인카메라
private float speed = 0.5f; // 이동속도
void Start()
{
}
void Update()
{
MoveLookAt();
}
void MoveLookAt()
{
//메인카메라가 바라보는 방향입니다.
Vector3 dir = cam.transform.localRotation * Vector3.forward;
//카메라가 바라보는 방향으로 팩맨도 바라보게 합니다.
transform.localRotation = cam.transform.localRotation;
//팩맨의 Rotation.x값을 freeze해놓았지만 움직여서 따로 Rotation값을 0으로 세팅해주었습니다.
transform.localRotation = new Quaternion(0, transform.localRotation.y, 0, transform.localRotation.w);
//바라보는 시점 방향으로 이동합니다.
gameObject.transform.Translate(dir * 0.1f * Time.deltaTime );
}
}
응용 > 텍스트 및 스크롤바와 연계
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MoveTowards : MonoBehaviour
{
/* 시점 방향으로 이동하는 스크립트입니다. */
public Camera cam; //메인카메라
private Transform camTr; //카메라 회전정보
private float speed = 0.5f; //pacman 이동속도
private bool isgameStart = false; //스크롤바 완성 시 게임 시작시키는 플래그
public UnityEngine.UI.Scrollbar obj_scrollbar_;
public UnityEngine.UI.Text obj_text;
void Start()
{
//카메라 Transform정보
camTr = Camera.main.GetComponent<Transform>();
}
void Update()
{
//스크롤바가 다 차면
if (obj_scrollbar_.size == 1.0f) {
//게임시작시키는 플래그를 true로 바꿔주고
isgameStart = true;
//스크롤바와 텍스트를 비활성화시킵니다.
obj_scrollbar_.gameObject.SetActive(false);
obj_text.gameObject.SetActive(false);
}
if (isgameStart)
MoveLookAt();
}
void MoveLookAt()
{
//메인카메라가 바라보는 방향입니다.
Vector3 dir = cam.transform.localRotation * Vector3.forward;
//카메라가 바라보는 방향으로 팩맨도 바라보게 합니다.
transform.localRotation = cam.transform.localRotation;
//팩맨의 Rotation.x값을 freeze해놓았지만 움직여서 따로 Rotation값을 0으로 세팅해주었습니다.
transform.localRotation = new Quaternion(0, transform.localRotation.y, 0, transform.localRotation.w);
//바라보는 시점 방향으로 이동합니다.
gameObject.transform.Translate(dir * 0.1f);
}
}
반응형