Camera explosion effect

Hi!
been trying to make my camera shake when explosions appear, i have got it working but it is not so accurate on getting back to same position.
for instance when shakeing it sometimes moves the view by X and Y values. I hope you know what I mean

life -= Time.deltaTime;
		if(life<= 0.0f  shake)
		{
                 shake = false;
		life = 3.0f;	
		}
		if(shake)
		{
	    cam.transform.position =  cposition + new Vector3(Random.value,Random.value,0);
		}

I want that to… help anyboby

I would declare variables that stored the camera’s eulerAngles and then when the shake event was off re apply the stored angles to the cameras transform.

This is quite a nice application of a coroutine. The Shake function below is wrapped in a script to demonstrate it, but you should just be able to copy it to your camera script to add the shaking effect.

var shakeNow: boolean;
var shakeTime: float;
var shakeDist: float;
var neutralPos: Vector3;

function Update () {
	if (shakeNow) {
		shakeNow = false;
		Shake(shakeTime, shakeDist);
	}
}


function Shake(shakeTime: float, shakeDistance: float) {
	var startTime: float = Time.time;
	neutralPos = transform.position;
	
	while (Time.time < startTime + shakeTime) {
		transform.position = neutralPos + Random.insideUnitSphere * shakeDistance;
		yield;
	}
	
	transform.position = neutralPos;
}

Note that this works from a fixed camera position. If your camera is moving when the shake is applied, you’ll have to update the neutral position from your movement code (essentially, it’s just the position where the camera would be without the shake).

1 Like