๐ ํ์ต๊ณผ ๊ฐ๋ฐ ๊ธฐ๋ก/Unity
ํน์ ๊ฑด๋ฌผ์ ๋ง์ปค ์์ฑํ๊ธฐ
cliche-2
2021. 8. 12. 23:27
728x90
์ฐธ๊ณ
MarkerTest.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class MarkerTest : MonoBehaviour
{
public Image image; // Marker
public Transform target; // Building
public Vector3 offset;
void Update()
{
// z index ์์ด์ ๊ณ ์ ์ ๋ฐ๋ฅธ ๋ง์ปค ์ด๋ ์์
float minX = image.GetPixelAdjustedRect().width / 2;
float maxX = Screen.width - minX;
float minY = image.GetPixelAdjustedRect().height / 2;
float maxY = Screen.width - minY;
// Temporary variable to store the converted position from 3D world point to 2D screen point
Vector2 pos = Camera.main.WorldToScreenPoint(target.position + offset);
// Check if the target is behind us, to only show the icon once the target is in front
if (Vector3.Dot((target.position - transform.position), transform.forward) < 0)
{
if (pos.x < Screen.width / 2)
{
pos.x = maxX;
}
else
{
pos.x = minX;
}
}
pos.x = Mathf.Clamp(pos.x, minX, maxX);
pos.y = Mathf.Clamp(pos.y, minY, maxY);
image.transform.position = pos;
}
}
ํด๋น ์ฝ๋๋ ์์ผ์ ํ๊น ๋น๋ฉ์ด ์๋์ง ํ์ธํ๊ณ , ํ๊น ๋น๋ฉ์ ๋ง์ปค ์ด๋ฏธ์ง๋ฅผ ๋ฃ๋ ์คํฌ๋ฆฝํธ์ด๋ค.
๋ฐ๋ผ์ ์คํฌ๋ฆฝํธ๋ MainCamera์ ์ถ๊ฐํ๋ค.
ํฌํจ๋ ํ๊น ๋ณ์๊ฐ 2๊ฐ ์๋๋ฐ, image์๋ ๋ง๋ ๋ง์ปค ์ด๋ฏธ์ง๋ฅผ ๋ฃ์ด์ค๋ค. target์๋ ๋ง์ปค๋ฅผ ์ง์ ํด์ค ๋น๋ฉ ์ค๋ธ์ ํธ๋ฅผ ๋ฃ์ด์ค๋ค.
728x90