Posts Free Look Camera (자유 시점 카메라)
Post
Cancel

Free Look Camera (자유 시점 카메라)

Note


  • 자유롭게 회전, 이동이 가능한 자유 시점 카메라

How To Use


  • 카메라가 존재하는 게임오브젝트에 FreeLookCamera 컴포넌트를 추가한다.
  • 게임 시작 후, Left Alt 키를 눌러 커서가 사라지도록 한다.
  • WASD 키로 카메라를 움직이며, 마우스 이동을 통해 카메라를 회전시킬 수 있다.
  • QE 키로 카메라를 상하로 움직일 수 있다.

Preview


2021_0228_FreeLookCam

Download


Source Code


.
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

// 날짜 : 2021-02-28 PM 5:30:50
// 작성자 : Rito

namespace Rito
{
    /// <summary> 자유 시점 카메라 </summary>
    [DisallowMultipleComponent]
    public class FreeLookCamera : MonoBehaviour
    {
        /***********************************************************************
        *                               Public Fields
        ***********************************************************************/
        #region .
        [Header("Options"), Range(1f, 20f)]
        public float _moveSpeed = 5f;
        [Range(1f, 10f)]
        public float _rotationSpeed = 5f;
        public bool _wheelAcceleration = true; // 마우스 휠로 이동속도 증가/감소

        [Space]
        public KeyCode _moveForward = KeyCode.W;
        public KeyCode _moveBackward = KeyCode.S;
        public KeyCode _moveLeft = KeyCode.A;
        public KeyCode _moveRight = KeyCode.D;
        public KeyCode _moveUp = KeyCode.E;
        public KeyCode _moveDown = KeyCode.Q;

        [Space]
        public KeyCode _run = KeyCode.LeftShift;
        public KeyCode _cursorLock = KeyCode.LeftAlt;

        [Header("States")]
        public bool _isActivated = true;     // 활성화 플래그
        public bool _isCursorVisible = true;

        #endregion
        /***********************************************************************
        *                               Private Fields
        ***********************************************************************/
        #region .

        private Vector3 _moveDir;
        private Vector3 _worldMoveDir;
        private Vector2 _rotation;

        private Transform _rig;
        private float _deltaTime;

        #endregion
        /***********************************************************************
        *                               Unity Events
        ***********************************************************************/
        #region .
        private void Awake()
        {
            InitRig();
            InitTransform();
        }

        private void Update()
        {
            if(!_isActivated) return; // 기능 비활성화 상태에서는 모든 기능 정지

            CursorLock();
            if (_isCursorVisible) return; // 커서 보이는 상태에서는 이동, 회전 X

            _deltaTime = Time.deltaTime;
            GetInputs();
            Rotate();
            Move();
        }

        #endregion
        /***********************************************************************
        *                               Init Methods
        ***********************************************************************/
        #region .
        private void InitRig()
        {
            _rig = new GameObject("Free Look Camera Rig").transform;
            _rig.position = transform.position;
            _rig.localEulerAngles = new Vector3(0f, transform.localEulerAngles.y, 0f);

            _rig.SetSiblingIndex(transform.GetSiblingIndex());
        }

        private void InitTransform()
        {
            transform.localEulerAngles = new Vector3(transform.localEulerAngles.x, transform.localEulerAngles.y, 0f);
            transform.SetParent(_rig);
        }

        #endregion
        /***********************************************************************
        *                               Private Methods
        ***********************************************************************/
        #region .
        private void GetInputs()
        {
            // 1. Movement
            _moveDir = new Vector3(0, 0, 0);

            if (Input.GetKey(_moveForward))  _moveDir.z += 1f;
            if (Input.GetKey(_moveBackward)) _moveDir.z -= 1f;
            if (Input.GetKey(_moveRight)) _moveDir.x += 1f;
            if (Input.GetKey(_moveLeft))  _moveDir.x -= 1f;
            if (Input.GetKey(_moveUp))   _moveDir.y += 1f;
            if (Input.GetKey(_moveDown)) _moveDir.y -= 1f;

            if (Input.GetKey(_run))
            {
                _moveDir *= 2f;
            }

            // 가속/감속
            if (_wheelAcceleration)
            {
                float wheel = Input.GetAxisRaw("Mouse ScrollWheel");
                if (wheel != 0)
                {
                    _moveSpeed += wheel * 10f;
                    if(_moveSpeed < 1f)
                        _moveSpeed = 1f;
                }
            }

            // 2. Rotation
            float mX = Input.GetAxisRaw("Mouse X");
            float mY = -Input.GetAxisRaw("Mouse Y");

            _rotation = new Vector2(mY, mX);
        }

        private void CursorLock()
        {
            if (Input.GetKeyDown(_cursorLock))
            {
                _isCursorVisible = !_isCursorVisible;

                Cursor.lockState = !_isCursorVisible ? CursorLockMode.Locked : CursorLockMode.None;
                Cursor.visible = _isCursorVisible;
            }
        }

        private void Rotate()
        {
            if(_rotation == Vector2.zero) return;

            float rotSpeed = _rotationSpeed * _deltaTime * 50f;

            Vector3 trnEuler = transform.localEulerAngles;
            Vector3 rigEuler = _rig.localEulerAngles;

            // Rotate X(Up & Down)
            float nextX = trnEuler.x + _rotation.x * rotSpeed;

            if(nextX > 180f) nextX -= 360f;
            nextX = Mathf.Clamp(nextX, -89.9f, 89.9f);

            transform.localEulerAngles = new Vector3(nextX, trnEuler.y, trnEuler.z);

            // Rotate Y(Left & Right)
            float nextY = rigEuler.y + _rotation.y * rotSpeed;
            _rig.localEulerAngles = new Vector3(rigEuler.x, nextY, rigEuler.z);
        }

        private void Move()
        {
            if(_moveDir == Vector3.zero) return;

            _worldMoveDir = transform.TransformDirection(_moveDir);
            _rig.Translate(_worldMoveDir * _moveSpeed * Time.deltaTime, Space.World);
        }

        #endregion
        /***********************************************************************
        *                               Public Methods
        ***********************************************************************/
        #region .

        /// <summary> 전체 기능 활성화/비활성화 </summary>
        public void Activate(bool state)
        {
            _isActivated = state;
        }

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