Forum rules - please read before posting.

Custom script: Shakycam

Hey guys, here's a little script I cooked up recently, maybe someone else will find it useful aswell.

It simulates the smooth shake of a portable camera, as seen in some modern action movies. If you want to make it look like your camera is handheld or if something was filmed in a moving car, this is an easy script to do so.

Installation is straightforward - drag it into your unity project and add the PerlinShake script to the Main Camera gameobject. The default values should give you a good subtle shake, but you can modify them to intensify the speed or the magnitude of the shake.

The script contains two functions, StartShake(); and StopShake();, to manually control the shake from code. If you check Play On Awake in the inspector the shake starts right away.

The Top Down checkbox is for when you are using the Topdown mode in Adventure Creator (it flips the Y and Z axis so the camera shakes in the correct directions).

The script is open for improvements, I sort of hacked the duration into it by setting the timer to 9999, if you know a better way feel free to modify it.

Enjoy!


Comments

  • Cool extension Tim, tx bro!
  • Hey hedgefield.

    This seems very useful. Say, I use to edit AC script to increase camera shake to have a value of 100 in slide bar so i can slide to end and really shake the camera. So will this enable be to shake the cam as like the native Camera Shake in AC but with a harsher movement?

    If so it will really ease my process altogether.
  • Yes I think you can do that. The AC shake action controls magnitude and duration with one slider I think, here you can set those values individually so you can tweak it how you like. Note: I hid the duration variable from the inspector, you'll have to make that public again in the code, but then you can use it the way you want I think.
  • Was looking for a solution for exactly this! Thanks for sharing.

    Having an issue with it, though. It seems to drop the camera to the floor as soon as the scene starts. Any idea why that would happen? It happens when assigned to the 3D demo scene as well.
  • Yes... isolating the horizontal shake works as expected. But shaking on the Y seems to drop the camera down to the floor. If I find a solution I'll post it - but I'm sure you would have noticed this behaviour - so wondering if I have something else wrong.
  • Hmm that's weird. I haven't seen this happen, though I've only tested in on 2D modes in AC, but I can't imagine it being much different in 3D. Have you unchecked TopDown on the script? And it should automatically find the right camera, but try putting it on a gamecamera instead of the main camera to see if that helps.

    If the problems persists I'll install the 3D demo and have a look. Anyone else experiencing this?
  • Hi hedgefield,

    Thanks for your response. I can tell you that:

    1. it is definitely finding the correct camera
    2. I've tried it both on game gameras and the main camera with identical results
    3. not in TopDown mode

    When I substitute the original camera Y instead of computed Y everything works (though obviously the shake is limited to the X axis).

    I can find my way around the code pretty easily - but I'm not great at the math. It appears that the Y-calculation is using 0 as the starting point instead of the camera's Y. I'll dig a little more later - but that seems to be practically what's happening.
  • Ah that could be. All my cameras (or at least the ones that shake) are set at the Y 0 position, so that might be why I never noticed that behaviour. I'll take a look too to see if I can fix that. Let me know what you find.
  • This is probably not the "right" solution, but it works:

    activeCamera.transform.position = new Vector3(x, originalCamPos.y + y, originalCamPos.z);
  • Okay, I actually needed to add the originalCamPos.x to the computed X as well to keep this reliable in my scenes.

    I'm trying to work on some code that will allow graceful camera switching when using transitions. Right now if you're using this script and doing any kind of camera animation you will definitely want to stop the shaking effect, animate the camera, then restart the shaking effect to avoid improper camera transforms from getting buffered.
  • Glad you got it sorted out. Can you PM me the updated code bits? I'll put them in the original script. 

    And yep it doesn't play well with animated cameras, I forgot to put that in the OP. It's because it transforms the cameraposition itself, instead of adding it to whatever motion it's doing. Back in the day I used to work around that by putting the camera into another parent object and animating that one, but I don't think it could work that way in AC.
  • I tested dxmachina's solution and it works fine in Unity2D mode also, so I'm going to assume this fixes the issue. I've updated the download in the OP to include his fix. If anyone has problems with TopDown mode because of this, let me know.
  • Something changed in the AC namespace with 1.38, so I updated the script to properly refer to it, should work again now. Thanks for the heads-up dxmachina.
  • How does this script differ from AC camera shake ?
  • When I made this, the AC shake was pretty basic and could only shake up to 10 seconds. This script can shake infinitely (well, up to 2.5 hours I think) and has settings to control the intensity and direction. That said, I don't know if it still works correctly since it was made before Unity 5 came out.
  • Alright thanks for the quick response
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Welcome to the official forum for Adventure Creator.
Do NOT follow this link or you will be banned from the site!