• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
7
Question by GreyWolf · Jan 31, 2012 at 06:10 PM · camerapositionlocalshake

Camera shake

Hey guys how can i make a simlpe camera shake that returns 2 its original position after shaking

 function Update() {
  
     if(Shake < 0){
     
      Shake =  0;
       
        } 
 
   if (Shake > 0) {
     
        cam.transform.localPosition = Random.insideUnitSphere * shakeAmount; 
     
             Shake -= Time.deltaTime * decreaseFactor;
 
 
   } else {
     shake = 0.0;
   }
 }
Comment
Add comment · Show 5
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Berenger · Jan 31, 2012 at 07:19 PM 0
Share

For a simple way, try that. For something a little more heavy, might be a little to much if you just want the camera shake : ShakePosition de iTween.

avatar image fafase · Jan 31, 2012 at 07:21 PM 0
Share

You could use a sin function with a damping constant. (Sounds scientic!!!)

Ae^(-kt) cos(wt)

e^(-kt) means e to the power of (-kt). This function will creates a large oscillation depending on you A that will reduce in time to finish to 0. The duration and tilting of your camera depends on the (-kt).

That might not be what you want but if that can lead you somewhere.

avatar image Fattie · Jan 31, 2012 at 07:28 PM 0
Share

You have to save the original position bud. There's no other way.

Your randomisation using the unit sphere is perfect: BUT you have to do "two jiggles" at a time. Always move it using your current system, and then in the new step simply return it to where it was.

Just use parity (i.e. odd/even) to know which you are supposed to do, ok

avatar image insominx · May 17, 2013 at 08:20 PM 0
Share

This blog covers it pretty well: http://unitytipsandtricks.blogspot.com/2013/05/camera-shake.html

avatar image Fattie · Jun 24, 2015 at 05:07 PM 0
Share

For the record it is extremely hard to actually program the physics of hand-held camera shake, "wind" shake, tripod shudder, and other versions of camera shake. Note that in unity you can simply attach a spring to the camera and let physX do the work, no coding required. This would be a winning idea since, uh, you pay $4500 for Unity "since it has a physics engine". So use it. By no means does a "spring on a camera" produce really realistic "camera shake", but it's better than using Perlin Noise. Perlin Noise is sort of ok; the other "solutions" on here are silly.

Getting back to the actual question asked by the OP, conceptually yes you have to save the "original" position (indeed the "base" position, which in any non-trivial system will, of course, be moving. You have to have a whole complicated system representing where the camera is "ideally" and so on.)

11 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by idbrii · Feb 28, 2021 at 06:51 AM

If you put your object under a parent that sets the world position, you can offset the localPosition to give it shake and when your offset goes to zero your object will return to the neutral position.

Please don't use random values to cause camera shake! Use a continuous function like perlin noise or sine to get smooth movement.

Here's a script ShakeIn2D that can shake objects or camera as shown in these videos.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
  • ‹
  • 1
  • 2
  • 3

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

24 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Constant camera shake. 1 Answer

How to move camera up and down 1 Answer

Is it possible to shake the camera? 4 Answers

Is there an elegant way of limiting camera's position? 1 Answer

How to freeze the rotation or the position on the camera transform? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges