티스토리 뷰

카테고리 없음

[Unity] 방향 지시 UI

Kim2558 2018. 10. 29. 21:57
화살표(방향 지시 UI)를 목표로 이동시킨 뒤 카메라 범위를 넘어간 거리를 비례식을 통해 변경

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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public class Navigation : MonoBehaviour {
 
    public float arrowSpeed;
    public Transform currentLocation;
    public Transform target;
 
    // Update is called once per frame
    void Update ()
    {
        ArrowHint();
    }
 
    private void ArrowHint()
    {
 
        Vector3 pos = Camera.main.WorldToViewportPoint(target.position);
 
        if ((pos.x < 1.0f && pos.x > 0.0f) && (pos.y < 1.0f && pos.y > 0.0f))
        {
            transform.position = Camera.main.ViewportToWorldPoint(pos);
            return;
        }
 
        pos *= 2.0f;
        pos = new Vector3(pos.x - 1, pos.y - 1, pos.z - 1);
 
        if (Mathf.Abs(pos.x) > Mathf.Abs(pos.y))
        {
            pos.y = pos.y / Mathf.Abs(pos.x);
            if (pos.x > 1)
                pos.x = 1;
            else if (pos.x < -1)
                pos.x = -1;
        }
        else
        {
            pos.x = pos.x / Mathf.Abs(pos.y);
            if (pos.y > 1)
                pos.y = 1;
            else if (pos.y < -1)
                pos.y = -1;
        }
        pos = new Vector3(pos.x + 1, pos.y + 1, pos.z + 1);
        pos /= 2.0f;
        transform.position = Vector3.Lerp(transform.position , Camera.main.ViewportToWorldPoint(pos), Time.deltaTime * arrowSpeed) ;
        transform.LookAt2D(target);
    }
}
cs


댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
TAG
more
«   2025/02   »
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
글 보관함