- Home /
Camera Shake while following Player?
Currently I have this script attached to an empty game object which follows the player around. The camera is also a child of this object.
using UnityEngine;
using System.Collections;
public class CameraShake : MonoBehaviour
{
public float duration = 0.5f;
public float speed = 5.0f;
public float magnitude = 0.2f;
void Update()
{
if (Input.GetKeyDown("f"))
{
PlayShake();
}
}
//This function is used outside (or inside) the script
public void PlayShake()
{
StopAllCoroutines();
StartCoroutine("Shake");
}
private IEnumerator Shake()
{
float elapsed = 0.0f;
Vector3 originalCamPos = transform.position;
Debug.Log(originalCamPos);
float randomStart = Random.Range(-1000.0f, 1000.0f);
while (elapsed < duration)
{
elapsed += Time.deltaTime;
float percentComplete = elapsed / duration;
float damper = 1.0f - Mathf.Clamp(1.5f * percentComplete - 1.0f, 0.0f, 1.0f);
float alpha = randomStart + speed * percentComplete;
float x = Mathf.PerlinNoise(alpha, 0.0f) * 2.0f - 1.0f;
float z = Mathf.PerlinNoise(0.0f, alpha) * 2.0f - 1.0f;
x *= magnitude * damper;
z *= magnitude * damper;
x += originalCamPos.x;
z += originalCamPos.z;
transform.position = new Vector3(x, originalCamPos.y, z);
yield return 0;
}
transform.position = Vector3.Lerp(transform.position, originalCamPos, Time.deltaTime * 5f);
}
}
However, as the player moves, while the camera is shaking, it doesn't follow the player. It only updates its position after the shake has ended. I know the error is when grabbing the original position and lerping to that after the shake, but I'm not sure how to update that to the player's position. Any thoughts?
Answer by Jeff-Kesselman · May 17, 2014 at 01:32 AM
Im not sure why you are doing this in a co-routine. It would be much simpler and less obtuse just to have a shake component that jitters the camera in its Update.
BUT doing it this way you will have to acquire the camera's current position at the start of the while loop because thats where the code restarts after your yield.
Wow, what a simple fix. Thanks for pointing it out.
I used a co-routine because I wanted to control the duration using a while loop. I'm not sure if it's the best practice, but it's what came into $$anonymous$$d.
If i would implement this in Update, would I need some sort of time stamp system in place?
Yes, so typically when you are doing timed things in Update you keep some kind of time value in a class field.
For instance, for an effect that yo unwanted to end in 3 seconds...
public class TimedEffect:$$anonymous$$onoBehaviour{
private float effectTimer= 0;
public void StartEffect(){
effectTimer = 3;
}
void Update(){
if (effectTimer>0){
effectTimer -= Time.deltaTime;
// increment effect below....
}
}
}
In general, when working with a game engine, it is better to use the game engine's game loop then to create your own loops on the side.
Your answer
Follow this Question
Related Questions
Convert type UnityEngine.Vector3' to float' Error 1 Answer
How to get the forward vector normal to the camera's forward vector regardless of camera pitch 1 Answer
Tracking Vertical Angle between Camera and Object 0 Answers
Keep camera at certain distance from character when rotating 3 Answers
Is it possible to shake the camera? 4 Answers