Post

KDT Unity 5주차 팀 프로젝트

2024-01-25 TIL

프레임 기준으로 후에 따라오게 하기

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
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Follower : MonoBehaviour
{
    public Vector3 followPos;
    public int followDelay;
    public Transform parent;
    public Queue<Vector3> parentPos;

    void Awake()
    {
        parentPos = new Queue<Vector3>();
    }

    // Update is called once per frame
    void Update()
    {
        Watch();
        Follow();
    }

    private void Watch()
    {
        //Queue = FIFO (First Input First Out)

        // Queue에 같은 위치가 있으면 저장하지 않도록 조건 추가
        if (!parentPos.Contains(parent.position))
        {
            //#.Input Pos
            parentPos.Enqueue(parent.position);
            Debug.Log("A");
        }

        // Queue에 일정 데이터 개수가 넘어가면 그때부터 반환
        if (parentPos.Count > followDelay)
        {
            //#.Output Pos
            followPos = parentPos.Dequeue();
            Debug.Log("B");
        }
        else if (parentPos.Count < followDelay)
        {
            followPos = parent.position;
            Debug.Log("C");
        }
    }

    private void Follow()
    {
        transform.position = followPos;
    }
}



This post is licensed under CC BY 4.0 by the author.