Camera Shake - Need Explanation on What's Happening

Hi

I have created a Shake script which will shake whatever transform it is attached to. However, it only shakes along a single plane. That is e.g. XZ plane.

I have a orthographic camera which is rotated 90 deg downwards to look at the XZ plane. When I attached the script to the Camera it behaves in an unexpected way. Instead of shaking in the XZ plane is shakes in the XY or ZY plane. This was weird. I realized that it is due to the camera rotation. So I fixed it by adding the camera to an empty GameObject which had no rotation.

This fixed the issue. But I would like to know if I could do this without involving another GameObject. Here is my code

private bool StartShake = false;
private Vector3 OrigianlPos;

public float Magnitude, ShakeDuration;
private float ShakeStartTime, ShakeStopTime;

void Shake(){
StartShake = true;
OriginalPos = transform.position;
ShakeStartTime = Time.time;
ShakeStopTime = ShakeStartTime + ShakeDuration;
}

void Update(){
if(StartShake){
if(Time.time >= ShakeStopTime){
StartShake = false;
transform.position = OriginalPos;
return;
}

Vector3 newPos = new Vector3(Random.Range(-Magnitude,Magnitude), 0.0f, Random.Range(-Magnitude,Magnitude));// XZ Plane
transform.position = newPos;
}
}

Unless I misunderstood the question, the important line in the script is this one:

Vector3 newPos = new Vector3(Random.Range(-Magnitude,Magnitude), 0.0f, Random.Range(-Magnitude,Magnitude));// XZ Plane

It’s creating a new Vector3 with random values (within a specified range) for the X and Z components. I’d say you could just change that to randomize the appropriate components of the vector (whatever those are) to get what you need.

Jeff

Well, if you already has a script that modify the camera position like a following script, you can fuse the two script and activate shaking only when you wants, or else, the camera position will be overwritten by the following script for example.

And if your camera is rotated, depending on how you ask it to move, it will use normally the camera axis or the world axis, which are not the same.

I tried all the components but still seem to be moving on the WORLDs XY plane (.I.e. Up and down) (Fix was to add the camera to another GameObject.
–EDIT–
And add the script to this GameObject
– END EDIT–
I just need to know what is the reason behind this.

Yes, The camera is rotated. I want the script to use the local axis not the world axis. How can I achieve this?

Here’s a quick little cam-shaker that I attach to my Camera gameObject.

The relevant part for you is the choice of the variable offset, line 26.

It uses the camera’s transform.rotation property to rotate a random displacement Vector3 in the X and Y planes only.

You can attach this to the standard Unity3D “SmoothFollow” script.

using UnityEngine;
using System.Collections;

public class CamShaker : MonoBehaviour
{
	public static float remainingShakeTime;
	public static float originalShakeTime;
	public static float maxDisplacement;

	public static void Shake( float amount, float time)
	{
		maxDisplacement = amount;
		originalShakeTime = time;
		remainingShakeTime = time;
	}

	void LateUpdate()
	{
		if (originalShakeTime > 0  remainingShakeTime > 0)
		{
			remainingShakeTime -= Time.deltaTime;

			float displacement = (maxDisplacement * remainingShakeTime) / originalShakeTime;

			// shake in direction of plane perpendicular to facing direction
			Vector3 offset = transform.rotation * (
				Vector3.right * Random.Range( -displacement, displacement) +
				Vector3.up * Random.Range( -displacement, displacement));

			transform.position += offset;
		}
	}
}

To shake your camera, do this:

	CamShaker.Shake( 0.2f, 0.1f);

It shakes more at first and then damps out (line 23). You can do other functions of increasing/decreasing the displacement over time if you prefer.

Kurt

Thanks guys…! But my question is not about how to do a Cam Shake. But why does the cam shakes in the world XY plane (After a rotation is applied) when instead of XZ plane is given as the components in the Vector3?

@Kurt Dekke: Thanks for the code snippet. I am new to vector math (more or less incompetent). Can you explain why you do a rotation and then add it to position? Thanks again

Sorry to bring up an old thread but when I use this code the camera doesn’t stop shaking. Any ideas?

Have you put the start time and remaining time in debug.logs to see what the values are as it’s shaking? Also this line:

if(originalShakeTime > 0 remainingShakeTime > 0)

is supposed to be:

if(originalShakeTime > 0 && remainingShakeTime > 0)

Thanks!