# Camera shake loop in main menu?

Hello,

My question would seem simple and may be discussed before, but so far couldn’t find a simple solution. I’d like to make a shaky camera preferably with a script in the main menu.

I guess it’s possible to animate, but I think with a script would be more flexible to attach and adjust to any camera?

Here’s the result I’m looking for: DiRT 2 - main menu - YouTube

Notice how the camera is hold by human hand and shakes a bit up and down.

``````public class MovingCamera : MonoBehaviour {
Vector3 origin;
Vector3 target;
float ratio=0.01f;
void Start(){
origin = transform.position;
InvokeRepeating("ChangeTarget",0.01f,2.0f);
}
void Update(){
transform.position = Vector3.Lerp (transform.position,target,ratio);
}
void ChangeTarget(){
float x = Random.Range(-1.0f,1.0f);
float y = Random.Range(-1.0f,1.0f);
target = new Vector3(origin.x+x,origin.y+y,origin.z);
}
}
``````

It takes the original camera position and starts lerping from one position to another. The new position are generated in ChangeTarget every 2 seconds. The new position is constrained in a square of 2X2 with the original point as center.

EDIT: Since it seems to be required in Js:

`````` var origin: Vector3;
var target: Vector3;
var ratio:float=0.01f;
function Start(){
origin = transform.position;
InvokeRepeating("ChangeTarget",0.01f,2.0f);
}
function Update(){
transform.position = Vector3.Lerp (transform.position,target,ratio);
}
function ChangeTarget(){
var x:float = Random.Range(-1.0f,1.0f);
var y:float = Random.Range(-1.0f,1.0f);
target = Vector3(origin.x+x,origin.y+y,origin.z);
}
}
``````