hi i m very new to unity i would like to know how would i go about coding a camera shake script to make the effect that the ground has shaken such as when a grenade explodes. any help would be good
You could either use math to animate the shake, or a short random wobbly/shaking animation attached to the camera and made to trigger on certain events.
If you plan on using iTween put your camera in a "holder" object since iTween will completely override all position values while its Shake() is running on the camera. Apply your iTween.ShakePosition() or iTween.ShakeRotation() or iTween.ShakeRotation on the contained camera and apply controller scripts on the holding object. Hope that wasn't horrible written and impossible to understand... gotta get some sleep.
And more than a year later, I've encountered the issue that I think you're referring to Not sure what you mean by "controller scripts on the holding object" though? I've been finding that when I use the shake, my camera slowly works its way out of position. As in, it doesn't return to the exact position it was in before the shake and gets further and further off center every time the shake is applied. I've been thinking about manually just recording the camera position, applying the shake, and then setting the camera back to the recorded position.
That's because you didn't do what pixelplacement1 said to do. iTween will make your camera drift away from its real position so that it won't return to exactly where it was when the shake started. You can fix this by creating a game object that you shake, and not the camera. You parent the camera to the game object either at the time you want the shake to happen or you do it at the beginning and put the game object at the origin.
Yeah... that's why I was asking what he meant... In the explanation here, he's saying that you still apply the shake to the "contained camera" and apply "controller scripts" on the holder. I wasn't sure what those controller scripts might be.
Whatever camera controller scripts you are using to currently move your camera around would be attached instead to a container game object that is the parent of your camera. The camera shake script would be on your camera.
That's exactly what I have set up, and the problem exists. I'm doing something wrong... I'll play around some more and see what I can figure out. Thanks!
If you have a game object that has the camera controller on it and the camera is a child of that object, you can reset the camera's local position back to origin once you have done with your shake. iTween offers callback functions that will invoke at the end of an animation, you can reset your origin in there.