Answers for "Unity how to make camera shake"

C#
0

Unity how to make camera shake

// Add this to a script on the camera

public void StartShake(float dur, float mag)
    {
        StartCoroutine(Shake(dur, mag));
    }

    IEnumerator Shake(float duration, float magnitude)
    {
        Vector3 OriginalPos = transform.position;
        float elapsed = 0.0f;
        while (elapsed < duration)
        {

            float x = Random.Range(-1f, 1f) * magnitude;
            float y = Random.Range(-1f, 1f) * magnitude;

            transform.localPosition = new Vector3(x, y, OriginalPos.z);
            
            elapsed += Time.deltaTime;
            yield return null;
        }
        transform.localPosition = OriginalPos;

    }
Posted by: Guest on June-12-2021

Code answers related to "Unity how to make camera shake"

C# Answers by Framework

Browse Popular Code Answers by Language