Post

KDT Unity 5주차 팀 프로젝트

2024-01-29 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
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using static UnityEngine.Rendering.DebugUI.Table;

public class CameraTest : MonoBehaviour
{
    [SerializeField] private GameObject _camera;
    [SerializeField] private Camera mainCam;
    [SerializeField] private Camera subCam;
    [SerializeField] private Vector3 _position1 = new (0.35f,-0.01f,0.05f);
    [SerializeField] private Vector3 _position2 = new(1, -0.5f, 1f);
    [SerializeField] private float rotateSpeed = 3f;  // 회전 속도
    private IEnumerator rotateCoroutine;
    private float speed = 5.0f; // 이동 속도
    private bool _cameraChenge;
    private bool _enabled = false;
    private bool _enabledRotate = false;
    private void Start()
    {
        _cameraChenge = Main.Game.OnCamera;
    }
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.G))
        {
            //Debug.Log("G 키가 눌렸습니다.");
            CameraMoveCheck();
        }
        if (Input.GetKeyDown(KeyCode.Alpha1))
        {
            Debug.Log("1 키가 눌렸습니다.");
            RotationCam(90);
        }
        if (Input.GetKeyDown(KeyCode.Alpha3))
        {
            RotationCam(-90);
        }
    }

    private void CameraMoveCheck()
    {
        if (!_enabled)
        {
            _enabled = true;
            StartCoroutine(CameraMoveCoroutine());
        }
    }

    private void RotationCam(float rot)
    {
        if(_cameraChenge && _enabledRotate)
        {
            rotateCoroutine = RotateObjectCoroutine(rot);
            StartCoroutine(rotateCoroutine);
        }
    }
    private IEnumerator RotateObjectCoroutine(float rot)
    {
        _enabledRotate = false;
        Quaternion startRotation = subCam.transform.rotation;
        Quaternion endRotation = startRotation * Quaternion.Euler(Vector3.forward * rot);
        float timeElapsed = 0;

        while (timeElapsed < rotateSpeed)
        {
            subCam.transform.rotation = Quaternion.RotateTowards(subCam.transform.rotation, endRotation, Time.deltaTime * 90 / rotateSpeed);
            timeElapsed += Time.deltaTime;
            yield return null;
        }

        subCam.transform.rotation = endRotation;
        _enabledRotate = true;
    }
    private IEnumerator CameraMoveCoroutine()
    {
        Vector3 cameraMovePoint = _cameraChenge ? _position2 : _position1;

        // 현재 위치와 목표 위치 사이의 거리 계산
        float distance = Vector3.Distance(_camera.transform.localPosition, cameraMovePoint);

        _cameraChenge = cameraMovePoint == _position1 ? true : false;
        if (!_cameraChenge)
        {
            StopCoroutine(rotateCoroutine);

            //해당 오브젝트의 로테이션값을 0,0,0으로 변경하고 싶다.

            subCam.enabled = _cameraChenge;
            mainCam.enabled = !_cameraChenge;
            _camera.SetActive(!_cameraChenge);
        }
        // 거리가 충분히 작으면 도착한 것으로 간주
        while (distance >= 0.001f)
        {
            // 현재 위치에서 목표 위치까지 일정 속도로 이동
            _camera.transform.localPosition = Vector3.MoveTowards(_camera.transform.localPosition, cameraMovePoint, speed * Time.deltaTime);
            distance = Vector3.Distance(_camera.transform.localPosition, cameraMovePoint);
            yield return null;
        }

        if (_cameraChenge)
        {
            subCam.enabled = _cameraChenge;
            mainCam.enabled = !_cameraChenge;

            _camera.SetActive(!_cameraChenge);
            _enabledRotate = true;
        }
        Debug.Log(_cameraChenge);
        _enabled = false; // 도착하면 이동을 멈춥니다.
    }



}

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