유니티

카메라 진동 효과

잼잼재미 2023. 12. 21. 17:01

position 변경 방법


  • 가장 일반적으로 사용하는 방법
  • MainCamera의 psition을 랜덤하게 이동

 

 

스크립트

[SerializeField] private float _shakeAmount = 3.0f;
[SerializeField] private float _shakeTime = 1.0f;
private void Start()
{
    StartCoroutine(Shake(_shakeAmount, _shakeTime));
}

IEnumerator Shake(float shakeAmount, float shakeTime)
{
    float timer = 0;
    while (timer <= shakeTime)
    {
        Camera.main.transform.position 
            = new Vector3 (UnityEngine.Random.insideUnitCircle.x * shakeAmount, UnityEngine.Random.insideUnitCircle.y * shakeAmount, -10);
        timer += Time.deltaTime;
        yield return null;
    }
    Camera.main.transform.position = new Vector3(0, 0, -10);
}

 

일반적으로 MainCamera의 위치의 z좌표는 -10으로 사용하는 경우가 많기때문에, z좌표는 고정해야 한다.

 

 

 

 

rotation 변경 방법


  • MainCamera가 Player를 따라 이동하는 경우, position 변경 방법을 사용할 수 없음
  • rotation을 변경해도 비슷한 진동효과를 낼 수 있음

 

 

스크립트

[SerializeField] private float _shakeAmount = 1f;
[SerializeField] private float _shakeTime = 0.5f;

private void Start()
{
    StartCoroutine(Shake(_shakeAmount, _shakeTime));
}

IEnumerator Shake(float shakeAmount, float shakeTime)
{
    float timer = 0;
    while (timer <= shakeTime)
    {
        Camera.main.transform.rotation = Quaternion.Euler((Vector3)UnityEngine.Random.insideUnitCircle * shakeAmount);
        timer += Time.deltaTime;
        yield return null;
    }
    Camera.main.transform.rotation = Quaternion.Euler(0f, 0f, 0f);
}

 

 

 

 

 

* UnityEngine.Random.insideUnitCircle : 반지름이 1인 원 안에서 위치를 Vector2로 가져옴

'유니티' 카테고리의 다른 글

Input 메서드 정리  (1) 2023.12.21
데미지 텍스트 효과  (0) 2023.12.21
칼라 변경  (0) 2023.12.20
인벤토리 만들기  (0) 2023.12.14
AI Navigation  (0) 2023.12.14