- Home /
Is it possible to shake the screen rather than shake the camera?
Hi, for my action game I've been implementing a method to create a screen shake effect that will be used when bullets are fired by the player.
So far I've successfully written a script that utilises perlin noise to move the camera. My problem, however, is that I am shaking a perspective camera, which means that objects further away shake far less than objects in the foreground when the script is enabled. This results in quite a weird effect and reduces the potency of the screen shake illusion.
Is there a way to actually shake the whole of the view (preferably without moving the GUI) instead of just the camera in Unity, meaning that all objects will shake at the same magnitude regardless of their distance from the camera? My game is effectively in 2.5D with a topdown view, with an additional camera which zooms in to the player, so (I'd think) objects do actually need to be at various y coordinates to achieve proper parallax.
Hopefully I've made myself clear, thanks in advance for helping!
$$anonymous$$ake a screenshot -> asign it to a sprite -> add a "shake animation" to the sprite.
@$$anonymous$$ Sorry, I should have been clearer that when I say my game's in 2.5D I mean that everything is rendered in 3D, but you play it on a 2D plane (the obvious example of a 2.5D game would be the New Super $$anonymous$$ario Bros. series). Just animating every object with the same shake animation will still yield the same issue as my original question - objects further away will appear to move less than objects in the foreground. And there's no way to tell how far away some objects will be from the camera ahead of time, so I cannot adjust the animation's magnitude accordingly.
Answer by ivoisbelongtous · Aug 20, 2014 at 12:09 PM
Thanks to @Eric5h5 pointing me in the direction of adjusting the camera projection, I have managed to make a very simple function that offsets the camera by the passed amount in the x and y directions -
void OffsetCameraObliqueness (float xOffset, float yOffset) {
float frustrumHeight = 2 * camera.nearClipPlane *
Mathf.Tan(camera.fieldOfView * 0.5f * Mathf.Deg2Rad);
float frustrumWidth = frustrumHeight * camera.aspect;
Matrix4x4 mat = camera.projectionMatrix;
mat[0, 2] = 2 * xOffset / frustrumWidth;
mat[1, 2] = 2 * yOffset / frustrumHeight;
camera.projectionMatrix = mat;
}
This is very useful for shaking the whole screen! I recommend you generate the x and y offsets using perlin noise to achieve a more natural shaking effect.
Please post a comment if you have any questions! And thanks a lot everyone who helped me out. :)
Sounds great! Could this be used on the StandardAssets ThirdPersonCamera? If so, how?
Answer by Eric5h5 · Aug 19, 2014 at 05:37 PM
You could move the vanishing point:
http://wiki.unity3d.com/index.php?title=OffsetVanishingPoint
Thanks a lot! Not sure how useful this will be in a commercial product with the CC BY-SA licensing though... (I'm not an expert in legal matters!)
Answer by grahnzz · Aug 19, 2014 at 04:36 PM
http://docs.unity3d.com/ScriptReference/Camera-rect.html
by changing Camera.rect x and y you would shake the whole rendered frame - note that u will have borders around the surface, you would probably have the surface a little bigger than the screen and keep your shakings in that range so no borders get visible
This is great, thanks! Can't believe I haven't seen this mentioned on any other screen shake topics, but maybe I just missed this detail. Giving this a shot now :)
Sorry, I'm struggling to find a way to make the Camera's viewport bigger than the screen itself. The normalised width and height seem to clamp at 1? Would it be possible to provide an example to show how to increase the camera's size in order to remove the borders? Or have I misunderstood your solution?
Actually no, it doesn't clamp, but centres the camera's view to the screen when possible. For example, shakeyCamera.rect = new Rect (-0.1f,0,1.1f,1f);
has no visible effect, even though the camera's x coordinate has been moved 10% to the left. Is there a way around this?
Your answer
Follow this Question
Related Questions
Camera Shake 4 Answers
Screen Shake Effect 7 Answers
How can i touch objects in perespective ? 0 Answers
iTween Shake intensity issues 1 Answer