- Home /
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: http://www.youtube.com/watch?v=PPGsGYahjd0
Notice how the camera is hold by human hand and shakes a bit up and down. Thanks a lot in advance!
Answer by fafase · Apr 12, 2013 at 08:40 PM
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.
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.
Your answer
Follow this Question
Related Questions
how to show cursor on specific time? 2 Answers
Loop a script ? 0 Answers
Loop animation issue. 2 Answers
Loop Animation In Script? 2 Answers
Best Practice for Menu System 1 Answer