- Home /
Is it possible to shake the camera?
So here is what I've got and what I'm trying to do. I have a pretty basic 2D platform game I'm trying to set up. At one point when the player touches a trigger it causes some debris to fall down and take out a platform ahead of the character. I have that running fine, but what I would like to do is cause the camera to shake for a second or two to simulate a small earthquake before the debris comes down.
I looked around and didn't see any scripts to do it already out there, and no similar questions on here. So is it possible? And if so any help on getting it working would be greatly appreciated.
This blog covers it pretty well: http://unitytipsandtricks.blogspot.com/2013/05/camera-shake.html
Answer by Eric5h5 · Sep 01, 2010 at 04:18 AM
Of course it's possible, you just program it to do that. This has a script with a camera shake function in it; it might work for an earthquake, but if not, then make another function that works the way you want.
Hey Eric I checked out that camera shake from that game example. I'm having trouble converting it to c though. The thing that has me stumped is the yeild. Am I supposed to make cameraShake return IEnumerator and replace the yeild with yield return new WaitForSeconds(0); ? And is this method just supposed to be called once or is it supposed to be inside update?
Also how do you cast a int to a bool in c heres the line that I'm having trouble with.
int shake = numberOfShakes; while (shake) {
I cant seem to get this to work, is there a trick to it?
Answer by Thom Denick · Mar 21, 2011 at 09:32 PM
You can easily implement this by using Unity's Animation system.
1.) Add an Animation Component to your camera if it doesn't already have one.
2.) Create a new animation for the camera.
3.) Add curves for Position.x, Position.y, Position.z
4.) Now create a few Keyframes along the first 50 or so frames.
5.) Go to each of your Keyframes and move each position curve slightly up and down, and play until you get it close to what you want. Save the animation.
All you have to do to call the animation via script (assuming it's assigned as the default anim on the camera) is:
myCamera.animation.Play();
Answer by Bora Kasap · Mar 06, 2011 at 08:26 AM
i'm using PC, could someone paste the codes here? :/ i made a vertical shaking effect like that, but i need a smoother one :/
////////////
var to_up : int = 0; var timer_up : int = 85; var timer_down : int =85;
function Update () { if (to_up == 1) { transform.Translate(Vector3.up Time.deltaTime/6); timer_up -= 1; } else if (to_up == 0) { transform.Translate(Vector3.up -Time.deltaTime/6); timer_down -= 1; }
if (timer_up == 0) { to_up = 0; timer_up = 85;}
if (timer_down == 0) { to_up = 1; timer_down = 85;}
}
///////////////
Answer by paulidis · Jul 14, 2011 at 05:41 PM
use itween by pixelplacement
iTween shake methods are terribly buggy: objects completely lose parent transforms while you use them.
Your answer
Follow this Question
Related Questions
Camera Shake on pressing Fire1 (used to fire bullets).. 0 Answers
iTween.ShakePosition freezes Rotation 0 Answers
2.5d camera control script 1 Answer
Camera Toggle script 1 Answer
Loop a script ? 0 Answers