🔍 Headbob이란?
사람이 움직일 때 시야가 흔들리듯이 이동시에 카메라가 흔들리는 효과
Player에 하위의 카메라를 담은 빈오브젝트 카메라 루트에다 스크립트를 붙여주었습니다.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Rendering;
public class HeadBob : MonoBehaviour
{
// Start is called before the first frame update
[Range(0.001f, 0.01f)]
public float Amount = 0.002f;
[Range(1f, 30f)]
public float Frequency = 10.0f;
[Range(10f, 100f)]
public float Smooth = 10.0f;
// Update is called once per frame
void Update()
{
CheckForHeadbobTrigger();
}
void CheckForHeadbobTrigger()
{
float headMagnitude = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical")).magnitude;
if(headMagnitude > 0)
{
StartHeadBob();
}
}
private Vector3 StartHeadBob()
{
Vector3 pos = Vector3.zero;
pos.y += Mathf.Lerp(pos.y, Mathf.Sin(Time.time * Frequency) * Amount * 1.4f, Smooth * Time.deltaTime);
transform.localPosition += pos;
return pos;
}
}
🌃CheckForHeadbobTrigger
먼저 Update에 캐릭터가 이동이 있는지 확인을 해야합니다.
이동 크기가 0보다 크다면 HeadBob을 실행해주어야합니다.
🌃StartHeadBob
headbob 실행 함수 입니다.
카메라를 흔들 pos변수를 zero로 초기화 해줍니다.
이게임에 상하만 흔들리게 하고 싶어서 pos.y를 사용합니다.
자연스러운 상하 흔들림을 위해 Mathf.Lerp로 현재값, 목표값, 보간속도를 넣어줍니다
현재값에 pos.y를 넣어줍니다
Frequency가 높을수록 빠르게 진동하고 Amount가 클 수록 흔들림의 크기를 조절합니다.
그리고 현재 transform에 pos를 더해주면 잘 적용이됩니다.
'Game Engine > Game Project' 카테고리의 다른 글
[Unity] 리팩토링 작업 (0) | 2025.04.25 |
---|---|
[Unity] 덜컹 거리는 움직임 (0) | 2025.04.18 |
[Unity] JsonUtility NewtonSoft JSON 차이 (0) | 2025.04.15 |
[Unity] Parsing (1) | 2025.04.14 |
[Unity] Setter 메서드 (0) | 2025.04.11 |