๊น์ ์
ํ์ธ๋ํ๊ต 22ํ๋ฒ ์ปดํจํฐ๊ณตํ๊ณผ
ํ์ธ๋ํ๊ต 22ํ๋ฒ ์ปดํจํฐ๊ณตํ๊ณผ
WorldToScreenPoint๋ 3D ์๋ ์ขํ๋ฅผ 2D ์คํฌ๋ฆฐ ์ขํ๋ก ๋ณํํ๋ ๋ฉ์๋๋ค. UI๋ฅผ 3D ์ค๋ธ์ ํธ ์์น์ ๋ง์ถ ๋ ์ด๋ค!
public Vector3 Camera.WorldToScreenPoint(Vector3 position);
using UnityEngine;
public class WorldToScreenExample : MonoBehaviour
{
public Camera mainCamera;
public Transform targetObject;
void Update()
{
Vector3 screenPos = mainCamera.WorldToScreenPoint(targetObject.position);
Debug.Log($"์คํฌ๋ฆฐ ์ขํ: {screenPos}");
}
}
๋ฉ์๋:
WorldToScreenPoint: ์๋ โ ์คํฌ๋ฆฐWorldToViewportPoint: ์๋ โ ๋ทฐํฌํธ3D ์ค๋ธ์ ํธ ์์ UI๋ฅผ ํ์ํ ๋ ์ฌ์ฉํ๋ค:
using UnityEngine;
using UnityEngine.UI;
public class WorldUI : MonoBehaviour
{
public Camera mainCamera;
public Transform targetTransform;
public RectTransform uiElement;
void Update()
{
// ์๋ ์ขํ๋ฅผ ์คํฌ๋ฆฐ ์ขํ๋ก ๋ณํ
Vector3 screenPos = mainCamera.WorldToScreenPoint(targetTransform.position);
// ์นด๋ฉ๋ผ ์์ ์์ ๋๋ง ํ์
if (screenPos.z > 0)
{
uiElement.gameObject.SetActive(true);
uiElement.position = screenPos;
}
else
{
uiElement.gameObject.SetActive(false);
}
}
}
private void LateUpdate()
{
if (targetTransform == null)
{
Destroy(gameObject);
return;
}
// ์๋ ์ขํ โ ์คํฌ๋ฆฐ ์ขํ ๋ณํ
Vector3 screenPosition = Camera.main.WorldToScreenPoint(targetTransform.position);
// UI๋ฅผ ์คํฌ๋ฆฐ ์ขํ + distance ์์น์ ๋ฐฐ์น
rectTransform.position = screenPosition + distance;
}
LateUpdate ์ฌ์ฉ ์ด์ : ์ค๋ธ์ ํธ ์์น๊ฐ Update์์ ๊ฐฑ์ ๋ ํ UI ์์น๋ฅผ ์ต์ข ์ ์ผ๋ก ๋ง์ถ๊ธฐ ์ํจ
WorldToScreenPoint๋ 3D ์ค๋ธ์ ํธ๋ฅผ 2D UI์ ์ฐ๊ฒฐํ ๋ ํ์๋ค. ์ฒด๋ ฅ ๋ฐ, ํ์คํธ ๋ง์ปค, ์กฐ์ค์ ๋ฑ์ ์ ์ฉํ๊ฒ ์ธ ์ ์๋ค!