KDT Unity 5주차 팀 프로젝트
2024-01-12 TIL
레이 캐스트를 활용하여 화면안에 헤드라인을 만들고 해당 해드라인에 있는 오브젝트 찾기
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
using UnityEngine;
public class Interact : MonoBehaviour
{
protected float maxCheckDistance;
protected LayerMask layerMask;
public Camera _camera;
public GameObject curInteractObject;
protected Outline curInteractOutline;
protected bool curInteractObjectCheck = false;
protected virtual void Update()
{
TargetingObject();
TargetingObjectCheck();
}
private void TargetingObject()
{
Ray ray = _camera.ScreenPointToRay(new Vector3(Screen.width / 2, Screen.height / 2));
RaycastHit hit;
//Debug.DrawRay(ray.origin, ray.direction * maxCheckDistance, Color.red);
if (Physics.Raycast(ray, out hit, maxCheckDistance, layerMask))
{
if (hit.collider != null)
{
if (curInteractOutline != null && hit.collider.gameObject == curInteractOutline.gameObject)
{
return;
}
if (curInteractOutline != null && curInteractOutline.gameObject != hit.collider.gameObject)
{
curInteractOutline.enabled = false;
}
curInteractObject = hit.collider.gameObject;
curInteractOutline = hit.collider.GetComponent<Outline>();
curInteractOutline.enabled = true;
}
}
else
{
if (curInteractOutline != null && !curInteractObjectCheck)
{
curInteractOutline.enabled = false;
curInteractOutline = null;
curInteractObject = null;
}
}
}
void TargetingObjectCheck()
{
if(curInteractObject != null)
{
Vector3 viewPos = _camera.WorldToViewportPoint(curInteractObject.transform.position);
if (viewPos.x >= 0 && viewPos.x <= 1 && viewPos.y >= 0 && viewPos.y <= 1 && viewPos.z > 0)
{
return;
}
else
{
curInteractObject.GetComponent<ReplayRecorder>().isClick = false;
curInteractOutline.enabled = false;
curInteractObject = null;
curInteractOutline = null;
}
}
}
}
This post is licensed under CC BY 4.0 by the author.
