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

+ Recent posts