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.
Thanks a lot in advance!

1 Answer

1

Here is something to start with:

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);
 }
}

Thank you! I see it's something clever to start with indeed, but unfortunately I get errors after adding "using UnityEngine; using System.Collections; " at the start.

oh you are using Javascript aren't you?

Yes, sir! Thank you. "Using" is the correct word, since I'm beginning to learn. I get two errors on line 12 and 13: float x = Random.Range(-1.0f,1.0f); float y = Random.Range(-1.0f,1.0f); UCE0001: ';' expected. Insert a semicolon at the end. Obviously it's not the semicolon.

Oops yep, I forgot those ones: var x:float = Random.Range(-1.0f,1.0f); var y:float = Random.Range(-1.0f,1.0f);

One other thing you could try to implement but then I am not sure of the result, would be to have the ratio of the lerp as a sin function with a max amplitude of maybe 0.02. This way you get rid of the slightly sudden move when the target changes. Since sin 0 is 0 and increase in time the movement should look smoother but I have not tried it to tell if it goes nicely.