camera shake

Discussion in 'Unity Support' started by jin76, Sep 3, 2010.

  1. jin76

    jin76

    Member

    Joined:
    Mar 4, 2010
    Messages:
    364
    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
  2. oblivionfeet

    oblivionfeet

    New Member

    Joined:
    Jul 24, 2010
    Messages:
    482
    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.
  3. SteveJ

    SteveJ

    Member

    Joined:
    Mar 26, 2010
    Messages:
    1,993
    iTween has a sweet camera shake built in. One line of code.
  4. pixelplacement1

    pixelplacement1

    New Member

    Joined:
    Aug 19, 2009
    Messages:
    674
    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.
  5. SteveJ

    SteveJ

    Member

    Joined:
    Mar 26, 2010
    Messages:
    1,993
    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.
  6. justinlloyd

    justinlloyd

    Member

    Joined:
    Aug 5, 2010
    Messages:
    1,657
    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.
  7. SteveJ

    SteveJ

    Member

    Joined:
    Mar 26, 2010
    Messages:
    1,993
    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.
  8. justinlloyd

    justinlloyd

    Member

    Joined:
    Aug 5, 2010
    Messages:
    1,657
    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.
  9. SteveJ

    SteveJ

    Member

    Joined:
    Mar 26, 2010
    Messages:
    1,993
    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!
  10. justinlloyd

    justinlloyd

    Member

    Joined:
    Aug 5, 2010
    Messages:
    1,657
    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.