Create Shake Camera Unity

Create Shake Camera Unity

To make the camera shake when it hits the ground in Unity, you can use the following steps:

  1. Create an empty GameObject and place it at the same position as the camera.
  2. Add a script to the empty GameObject and attach the script to the camera as a component.
  3. In the script, create a public float variable to store the intensity of the shake.
  4. When the camera hits the ground, call a function to start the shake effect.
  5. In the shake function, use Time.deltaTime to calculate how much the camera should shake based on the intensity.
  6. Use the transform.position property to move the camera position slightly in a random direction each frame.
  7. Decrease the intensity of the shake over time until it reaches zero.
  8. When the intensity reaches zero, stop shaking the camera.

Here is some sample code to achieve this effect:


public class CameraShake : MonoBehaviour
{
    public float shakeIntensity;
    public float shakeDecay;

    private Vector3 originalPos;
    private bool isShaking;

    void Start()
    {
        originalPos = transform.position;
    }

    void Update()
    {
        if (isShaking)
        {
            transform.position = originalPos + Random.insideUnitSphere * shakeIntensity;
            shakeIntensity -= shakeDecay * Time.deltaTime;
            if (shakeIntensity <= 0)
            {
                shakeIntensity = 0;
                isShaking = false;
                transform.position = originalPos;
            }
        }
    }

    public void Shake()
    {
        isShaking = true;
    }
}



To use this script, you can call the Shake() function when the camera hits the ground. You can adjust the shakeIntensity and shakeDecay variables to control the intensity and duration of the shake effect.