- Home /
Camera Shake Effect When Camera Following Player
I was working on a 3d game where camera is continuously following the player object.
Now when a bomb hits the player object I want to play camera shake with blast particle effect.
I have coded this for camera shake:
public IEnumerator PlayCameraShakeAnimation(float duration, float magnitude)
{
Vector3 originalPosition = transform.localPosition;
float elapsedTime = 0f;
while(elapsedTime < duration)
{
float x = Random.Range(-1f, 1f) * magnitude;
float y = Random.Range(-1f, 1f) * magnitude;
transform.localPosition = new Vector3(x, y,originalPosition.z);
elapsedTime += Time.deltaTime;
yield return null;
}
transform.localPosition = originalPosition;
}
Camera Follow is the normal script I have written to follow the player.
Because of Camera Follow script is running, camera shake effect we can't able to show in the screen.
If I turn off Camera Follow script then we can able to see clearly camera shaking otherwise not.
If I turn off Camera Follow script for small amount of time then I am losing smoothness in following of player because the player position gets updated after the blast. So when I start back following the player, it will get a high jerk in a movement to reach a target position.
Also, the Camera is following the player in position and rotation in both fields. Now provide me some suggestions to achieve the Camera Shake effect while following the player.
Answer by Ermiq · Nov 22, 2019 at 05:25 PM
Make the camera object a child of another object. Move your camera following script to the parent object. And leave the shake script at the camera object (the child).
With a setup like this your following script will be modifying the parent position and rotation while the camera shake script will be modifying local position and rotation of the child object.
Thank you @Ermiq this helped me too, also if anyone else having problem with follow camera (weird glitch, jitters etc.) after doing this, try with making camera a child of player, that solved some of my issues.