unity - Find Target Object like Movement Designator 이동지시처럼 타겟 오브젝트 찾기

playMode







---------------------------------------------
target


arrow - Set tag


pathPoint - set tag


Backgtound - Set Navigation static


Bake


Player Setting



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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
[RequireComponent(typeof(NavMeshAgent))]
public class MoveLine : MonoBehaviour {
    NavMeshAgent nav;
    public Transform target; // target Object Transfrom
    public GameObject arrowPrefab; // arrowPrefab
    public GameObject pathPointPrefab; // see meshPoint Path
    public int distanceOfEachObject;
    public bool completePath = false;
    public bool temp = false;
    private void Start()
    {
        nav = GetComponent<NavMeshAgent>();
        nav.isStopped = true;
    }
    void Update()
    {
        if (temp)
        {
            GameObject[] delObj = GameObject.FindGameObjectsWithTag("arrow");
            foreach (GameObject obj in delObj)
                Destroy(obj);
            completePath = false;
            temp = false;
        }
        OnDrawDirection(target);
    }
    
    void OnDrawDirection(Transform obj)
    {
        Vector3 posA, posB;
        
        if (nav == null || nav.path == null)
            return;
        
        nav.SetDestination(obj.position); // Destination 
                                          // issue : 첫 도착지를 설정할때 nav.path.Length가 1이다.
        var path = nav.path;
        
        if (path.corners.Length < 2)
            return;
        if (completePath)
            return;
        // Create Objects
        for (int i = 0; i < path.corners.Length-1; i++)
        {
            posA = path.corners[i];
            posB = path.corners[i + 1];
            float vectorDis = Vector3.Distance(posA, posB);  // distance A to B vector
            float repeatCount = vectorDis/ distanceOfEachObject;
            float linearInterpolation = 1 / repeatCount; // 선형보간
            // if vectorDis<1, create a object. center From A and B. 
            if (repeatCount < 1)
            {
                repeatCount = 1;
                linearInterpolation = 0.5f;
            }
            GameObject pathPos = Instantiate(pathPointPrefab);
            pathPos.transform.position = path.corners[i];
            pathPos.transform.SetParent(GameObject.Find("Nods").GetComponent<Transform>());
            // create object.
            for (int j = 1; j <= (int)repeatCount; j++)
            {
                Vector3 position = Vector3.Lerp(posA, posB, j * linearInterpolation); // position Vector
                GameObject arrowPosition = Instantiate(arrowPrefab); 
                arrowPosition.transform.position = position; // Set position
                arrowPosition.transform.LookAt(posB); // Set direction
                arrowPosition.transform.eulerAngles = new Vector3(90, arrowPosition.transform.eulerAngles.y); // Set angle
                arrowPosition.transform.SetParent(GameObject.Find("Nods").GetComponent<Transform>()); // Set hierarchy
            }
        }
        completePath = true// Done
    }
}
cs

댓글