반응형
SMALL
타겟을 바라보고 gaze가 꽉차게 되면 Physics.Raycast 함수를 이용해 RaycastHit 타입을 반환받아 해당 오브젝트의 월드 좌표를 가져와 플레이어의 위치를 변경해주는 방식으로 오브젝트로의 이동을 구현하였다.
using System.Collections;
using UnityEngine;
using UnityEngine.UI;
public class RayContorller : MonoBehaviour
{
public Transform trans;
private Ray ray;
private RaycastHit hit;
public float dist;
public Player player;
public Image img;
public GameObject detailPopup;
public GameObject popupPanel;
public RectMain rectMain;
private bool isLoading;
private bool isClicked;
private Image btnYes;
private Image btnNo;
private Image btnNO2;
public GameObject[] targets;
void Start()
{
this.trans = this.GetComponent<Transform>();
this.popupPanel = this.rectMain.uICArMenu.transform.Find("Menu").transform.Find("popupPanel").gameObject;
this.detailPopup = this.rectMain.uICArMenu.transform.Find("Menu").transform.Find("UIarbillboard").gameObject;
this.btnYes = this.popupPanel.transform.Find("yesBtn").GetComponent<Image>();
this.btnNo = this.popupPanel.transform.Find("noBtn").GetComponent<Image>();
this.btnNO2 = this.detailPopup.transform.Find("noBtn").GetComponent<Image>();
}
void Update()
{
this.ray = new Ray(this.trans.position, this.trans.forward);
//Debug.DrawRay(this.ray.origin, this.ray.direction * this.dist, Color.green);
var mask = 1 << 9 | 1 << 8;
if (Physics.Raycast(this.ray, out this.hit, this.dist, mask) && !this.isClicked)
{
if (this.hit.transform.gameObject.name.Contains("Car") && !this.detailPopup.activeSelf)
{
this.popupPanel.SetActive(true);
Debug.Log("차량에 맞음");
}
else if (!this.isLoading && !this.hit.transform.gameObject.name.Contains("Car"))
{
this.StartCoroutine(StartFillamount(this.hit.transform.gameObject));
}
}
else
{
this.isLoading = false;
this.img.fillAmount = 1;
}
RaycastHit hit2;
if (Physics.Raycast(this.ray, out hit2, this.dist))
{
Debug.Log("타겟에 맞음1");
if (hit2.transform.gameObject.tag == "target")
{
Debug.Log("타겟에 맞음2");
this.StartCoroutine(StartFillamount(hit2.transform.gameObject));
Debug.LogFormat("<color=red>맞은 오브젝트 이름 : {0}\n맞은 오브젝트 트랜스폼 : {1}</color>", hit2.transform.gameObject.name, hit2.transform.position);
}
}
}
public void MoveToTarget(Vector3 targetPos)
{
this.player.gameObject.transform.position = targetPos;
}
public IEnumerator StartFillamount(GameObject targetBtn)
{
if (targetBtn != null)
{
Debug.Log("StartFillamount 코루틴 시작");
this.img.fillAmount = 0;
this.isLoading = true;
while (isLoading)
{
if (this.img.fillAmount == 1)
{
if (targetBtn.tag == "target")
{
this.MoveToTarget(targetBtn.transform.position);
break;
}
if (targetBtn.name.Contains("no"))
{
Debug.Log("no에 맞음");
this.popupPanel.SetActive(false);
this.detailPopup.SetActive(false);
Color btnColor = targetBtn.GetComponent<Image>().color;
btnColor.a = 35f;
targetBtn.GetComponent<Image>().color = btnColor;
this.StartCoroutine(this.waitForSecons());
break;
}
else
{
this.isClicked = true;
this.StartCoroutine(this.waitForSecons());
this.detailPopup.SetActive(true);
this.popupPanel.SetActive(false);
Color btnColor = targetBtn.GetComponent<Image>().color;
btnColor.a = 35f;
targetBtn.GetComponent<Image>().color = btnColor;
break;
}
}
this.img.fillAmount += 1.0f / 100.0f;
if (!this.isLoading)
{
Color btnColor = targetBtn.GetComponent<Image>().color;
btnColor.a = 1f;
targetBtn.GetComponent<Image>().color = btnColor;
break;
}
yield return null;
}
this.isLoading = false;
}
}
public IEnumerator waitForSecons()
{
while (this.isClicked)
{
yield return new WaitForSeconds(3f);
this.isClicked = false;
break;
}
}
}
물체에 타겟에 부딪혔다는걸 가려내기위해 이번엔 Layer 가 아닌 태그를 이용하여 ray가 부딪힌 오브젝트를 가려냈다.
다음 복습시엔 UI 와의 상호작용을 ExcuteEvent 함수를 이용하여 실제 버튼 컴포넌트를 사용하여 구현하는 방법도 해봐야지...
반응형
LIST
'유니티 엔진 클라이언트 > 개인공부' 카테고리의 다른 글
| [유니티] Euler Angle (오일러 각) & Quaternion (쿼터니언) 정리 .1 (1) | 2023.03.01 |
|---|---|
| [유니티] 3D월드에서의 FPS 시점(1인칭) 움직임 구현 (0) | 2023.02.24 |
| [유니티] VRCity 3D virtual 팝업 & 코루틴이용 & RayCast & 비트연산자 & Quaternion.Lerp사용 캐릭터 회전 (0) | 2023.02.23 |
| [유니티] 캐릭터 + UI 좌표 동기화 (복습) (0) | 2023.02.19 |
| [유니티] 미션 데이터 직렬화 복습 (0) | 2023.02.15 |