
Hierarchy
โโโ SculptureObject โ GenArtSculpture.cs ๋ถ์ฐฉ
โโโ Main Camera โ OrbitCamera.cs ๋ถ์ฐฉ
โ target โ SculptureObject ๋๋๊ทธ ํ ๋น
[์ธํฐ๋์ ]
using UnityEngine;
using UnityEngine.InputSystem; // New Input System ๋ค์์คํ์ด์ค
public class OrbitCamera : MonoBehaviour
{
[Header("ํ๊ฒ")]
public Transform target;
[Header("์ด๊ธฐ ์์น")]
public float initialDistance = 12f;
public float initialPitch = 20f;
public float initialYaw = 30f;
[Header("๊ณต์ ์ค์ ")]
public float orbitSensitivity = 0.15f;
public float orbitSmoothing = 8.0f;
[Header("์ค ์ค์ ")]
public float zoomSensitivity = 20.0f;
public float zoomMin = 3.0f;
public float zoomMax = 200.0f;
public float zoomSmoothing = 8.0f;
[Header("์๋ ๊ณต์ ")]
public bool autoOrbit = true;
public float autoOrbitSpeed = 5.0f;
// ============================================================
// ๋ด๋ถ ์ํ
// ============================================================
float _yaw;
float _pitch;
float _distance;
float _targetYaw;
float _targetPitch;
float _targetDistance;
bool _isDragging = false; // ์ค๋ฅธ์ชฝ ๋ฒํผ ๋๋๊ทธ ์ค ์ฌ๋ถ
// New Input System์์ ๋ง์ฐ์ค ์ฐธ์กฐ๋ฅผ ์บ์ฑํฉ๋๋ค
Mouse _mouse;
Keyboard _keyboard;
// ============================================================
// Unity ์ด๋ฒคํธ
// ============================================================
void Start()
{
// ํ์ฌ ์ฐ๊ฒฐ๋ ๋ง์ฐ์ค์ ํค๋ณด๋ ์ฅ์น๋ฅผ ๊ฐ์ ธ์ต๋๋ค
// ์ฅ์น๊ฐ ์์ผ๋ฉด null์ด ๋ฐํ๋๋ฏ๋ก ์ดํ null ์ฒดํฌ๊ฐ ํ์ํฉ๋๋ค
_mouse = Mouse.current;
_keyboard = Keyboard.current;
_yaw = _targetYaw = initialYaw;
_pitch = _targetPitch = initialPitch;
_distance = _targetDistance = initialDistance;
ApplyTransform(true);
}
void LateUpdate()
{
if (target == null) return;
// ์ฅ์น ์ฐ๊ฒฐ์ด ๋๊ฒผ๋ค๊ฐ ๋ค์ ์ฐ๊ฒฐ๋ ์ ์์ผ๋ฏ๋ก ๋งค ํ๋ ์ ๊ฐฑ์ ํฉ๋๋ค
_mouse = Mouse.current;
_keyboard = Keyboard.current;
HandleOrbitInput();
HandleZoomInput();
HandleResetInput();
HandleAutoOrbit();
// ์ค๋ฌด๋ฉ: ํ์ฌ ๊ฐ์ ๋ชฉํ ๊ฐ ๋ฐฉํฅ์ผ๋ก ๋ณด๊ฐํฉ๋๋ค
_yaw = Mathf.LerpAngle(_yaw, _targetYaw, Time.deltaTime * orbitSmoothing);
_pitch = Mathf.Lerp (_pitch, _targetPitch, Time.deltaTime * orbitSmoothing);
_distance = Mathf.Lerp (_distance, _targetDistance, Time.deltaTime * zoomSmoothing);
ApplyTransform(false);
}
// ============================================================
// ์
๋ ฅ ์ฒ๋ฆฌ
// ============================================================
void HandleOrbitInput()
{
if (_mouse == null) return;
// New Input System: wasPressedThisFrame / wasReleasedThisFrame
// ์ค๋ฅธ์ชฝ ๋ง์ฐ์ค ๋ฒํผ ์ํ๋ฅผ ๊ฐ์งํฉ๋๋ค
if (_mouse.rightButton.wasPressedThisFrame)
_isDragging = true;
if (_mouse.rightButton.wasReleasedThisFrame)
_isDragging = false;
if (!_isDragging) return;
// New Input System: delta.value๋ก ์ด๋ฒ ํ๋ ์์ ๋ง์ฐ์ค ์ด๋๋์ ์ฝ์ต๋๋ค
// ๊ตฌ Input System์ GetAxis("Mouse X/Y")์ ํด๋นํฉ๋๋ค
Vector2 delta = _mouse.delta.ReadValue();
// ์ด๋๋์ Time.deltaTime์ ๊ณฑํด์ ํ๋ ์๋ ์ดํธ ๋
๋ฆฝ์ ์ผ๋ก ๋ง๋ญ๋๋ค
_targetYaw += delta.x * orbitSensitivity;
_targetPitch -= delta.y * orbitSensitivity;
_targetPitch = Mathf.Clamp(_targetPitch, -80f, 80f);
}
void HandleZoomInput()
{
if (_mouse == null) return;
// New Input System์ scroll ๊ฐ์ ํฝ์
๋จ์๋ก ๋ค์ด์ต๋๋ค.
// ์ผ๋ฐ์ ์ธ ํ ํ ์นธ = ์ฝ 120. ์ด๊ฑธ ๊ทธ๋๋ก ์ฐ๋ฉด ๋๋ฌด ํฌ๋ฏ๋ก
// 120์ผ๋ก ๋๋ ์ "ํ ์นธ = 1.0" ๋จ์๋ก ์ ๊ทํํฉ๋๋ค.
float scroll = _mouse.scroll.ReadValue().y / 120f;
if (Mathf.Abs(scroll) < 0.001f) return;
// ํ์ฌ ๊ฑฐ๋ฆฌ์ ๋น๋กํด์ ์ค ์๋๋ฅผ ์กฐ์ ํฉ๋๋ค.
// ๋ฉ๋ฆฌ ์์์๋ก ํ ๋ฒ์ ๋ ๋ง์ด ์ด๋ํด์ ์์ฐ์ค๋ฝ๊ฒ ๋๊ปด์ง๋๋ค.
_targetDistance -= scroll * zoomSensitivity * (_targetDistance * 0.2f);
_targetDistance = Mathf.Clamp(_targetDistance, zoomMin, zoomMax);
}
void HandleResetInput()
{
if (_keyboard == null) return;
// New Input System: wasPressedThisFrame์ผ๋ก ์ด๋ฒ ํ๋ ์ ํค ์
๋ ฅ ๊ฐ์ง
// ๊ตฌ Input System์ GetKeyDown()์ ํด๋นํฉ๋๋ค
if (!_keyboard.fKey.wasPressedThisFrame) return;
_targetYaw = initialYaw;
_targetPitch = initialPitch;
_targetDistance = initialDistance;
}
void HandleAutoOrbit()
{
if (!autoOrbit || _isDragging) return;
_targetYaw += autoOrbitSpeed * Time.deltaTime;
}
// ============================================================
// ์นด๋ฉ๋ผ Transform ์ ์ฉ
// ============================================================
void ApplyTransform(bool instant)
{
Quaternion rotation = Quaternion.Euler(
instant ? initialPitch : _pitch,
instant ? initialYaw : _yaw,
0f
);
float dist = instant ? initialDistance : _distance;
Vector3 offset = rotation * new Vector3(0f, 0f, -dist);
transform.position = target.position + offset;
transform.LookAt(target.position);
}
// ============================================================
// ๊ธฐ์ฆ๋ชจ
// ============================================================
#if UNITY_EDITOR
void OnDrawGizmosSelected()
{
if (target == null) return;
Gizmos.color = new Color(0.3f, 0.8f, 1f, 0.2f);
Gizmos.DrawWireSphere(target.position, _distance > 0 ? _distance : initialDistance);
Gizmos.color = new Color(1f, 1f, 0f, 0.5f);
Gizmos.DrawLine(target.position, transform.position);
}
#endif
}