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:
- Create an empty GameObject and place it at the same position as the camera.
- Add a script to the empty GameObject and attach the script to the camera as a component.
- In the script, create a public float variable to store the intensity of the shake.
- When the camera hits the ground, call a function to start the shake effect.
- In the shake function, use Time.deltaTime to calculate how much the camera should shake based on the intensity.
- Use the transform.position property to move the camera position slightly in a random direction each frame.
- Decrease the intensity of the shake over time until it reaches zero.
- 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.
Post a Comment
image video quote pre code