Post

KDT Unity 5주차 팀 프로젝트

2024-01-31 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
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;
    }

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

...

    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.