• 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 CreativeMatter · Jun 19, 2016 at 05:05 AM

Have a look at this thread

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
avatar image
0

Answer by Razzik · Oct 22, 2015 at 08:22 AM

Hi @squareorb Thanks so much for the solution. It works like a charm!

I thought I'd never find a fix for getting a good Camera Shake. Just a quick one though.

Do you know of a method to somehow make only the X Y or Z Axis shake when you shake the position. I've tried converting everything to floats by using the .x/.y/.z parameters on the vector3's but it doesn't seem to work.

Any Suggestions? Thanks again!

Kind Regards,

Comment
Add comment · Show 1 · 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
avatar image Razzik · Oct 22, 2015 at 08:21 AM 0
Share

Hi Guys and @squareorb,

Not to worry. I found a way to do it. Rookie error

Here it is if anyone wants to change the shake direction for any reason and they're not sure how.

 using UnityEngine; 
 using System.Collections;
 
 public class ShakeCamera : $$anonymous$$onoBehaviour { 
     
     public bool shakePosition;
     public bool shakeRotation;
     
     public float shakeIntensity = 0.5f; 
     public float shakeDecay = 0.02f;
     
     private Vector3 OriginalPos;
     private Quaternion OriginalRot;
     
     private bool isShakeRunning = false;
     
 
     public void DoShake()
     {
 
 
         OriginalPos = transform.position;
         OriginalRot = transform.rotation;
         
         StartCoroutine ("ProcessShake");
     }
     
     IEnumerator ProcessShake()
     {
         if (!isShakeRunning) {
             isShakeRunning = true;
             float currentShakeIntensity = shakeIntensity;
             
             while (currentShakeIntensity > 0) {
                 if (shakePosition) {
                     transform.position = new Vector3(Random.insideUnitSphere.x * currentShakeIntensity, transform.position.y ,transform.position.z);
                     // just change the float values of the Vector3 Component to Random.insideUnitSphere.x/y/z where you see fit.
                 }
                 if (shakeRotation) {
                     transform.rotation = new Quaternion (OriginalRot.x + Random.Range (-currentShakeIntensity, currentShakeIntensity) * .2f,
                                                          OriginalRot.y + Random.Range (-currentShakeIntensity, currentShakeIntensity) * .2f,
                                                          OriginalRot.z + Random.Range (-currentShakeIntensity, currentShakeIntensity) * .2f,
                                                          OriginalRot.w + Random.Range (-currentShakeIntensity, currentShakeIntensity) * .2f);
                 }
                 currentShakeIntensity -= shakeDecay;
                 yield return null;
             }
             
             isShakeRunning = false;
         }
     }
 }

As Commented:

// just change the float values of the Vector3 Component to Random.insideUnitSphere.x/y/z where you see fit.

I hope it helps anyone who comes across this.

Thanks again guys.

avatar image
0

Answer by squareorb · Jun 24, 2015 at 10:26 AM

I really liked this solution for what I'm working on so I thought I'd share the modifications I made. I added the ability to select from the inspector whether you just want to shake position, rotation or both. I also made the intensity and decay values public so they can be set from the inspector as well. And most importantly took the bulk of the code out of the update method and made it a coroutine. This keeps it from checking the shakeIntensity on every single frame whether shaking or not. Cheers!

 using UnityEngine; 
 using System.Collections;
 
 public class ShakeCamera : MonoBehaviour { 
 
     public bool shakePosition;
     public bool shakeRotation;
 
     public float shakeIntensity = 0.5f; 
     public float shakeDecay = 0.02f;
     
     private Vector3 OriginalPos;
     private Quaternion OriginalRot;
 
     private bool isShakeRunning = false;
 
     
     public void DoShake()
     {
         OriginalPos = transform.position;
         OriginalRot = transform.rotation;
 
         StartCoroutine ("ProcessShake");
     }
 
     IEnumerator ProcessShake()
     {
         if (!isShakeRunning) {
             isShakeRunning = true;
             float currentShakeIntensity = shakeIntensity;
 
             while (currentShakeIntensity > 0) {
                 if (shakePosition) {
                     transform.position = OriginalPos + Random.insideUnitSphere * currentShakeIntensity;
                 }
                 if (shakeRotation) {
                     transform.rotation = new Quaternion (OriginalRot.x + Random.Range (-currentShakeIntensity, currentShakeIntensity) * .2f,
                                                         OriginalRot.y + Random.Range (-currentShakeIntensity, currentShakeIntensity) * .2f,
                                                         OriginalRot.z + Random.Range (-currentShakeIntensity, currentShakeIntensity) * .2f,
                                                         OriginalRot.w + Random.Range (-currentShakeIntensity, currentShakeIntensity) * .2f);
                 }
                 currentShakeIntensity -= shakeDecay;
                 yield return null;
             }
 
             isShakeRunning = false;
         }
     }
 }
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
avatar image
2

Answer by sjaak45 · Jan 12, 2013 at 08:47 PM

Thanks, this works perfectly!

For testing I added a test button which calls the DoShake function:

 using UnityEngine;
 using System.Collections;
 
 public class CameraShake : MonoBehaviour {
     
 
 public bool Shaking; 
 private float ShakeDecay;
 private float ShakeIntensity;    
 private Vector3 OriginalPos;
 private Quaternion OriginalRot;
 
 void Start()
 {
     Shaking = false;   
 }
 
 
 // Update is called once per frame
 void Update () 
 {
     if(ShakeIntensity > 0)
     {
         transform.position = OriginalPos + Random.insideUnitSphere * ShakeIntensity;
         transform.rotation = new Quaternion(OriginalRot.x + Random.Range(-ShakeIntensity, ShakeIntensity)*.2f,
                                   OriginalRot.y + Random.Range(-ShakeIntensity, ShakeIntensity)*.2f,
                                   OriginalRot.z + Random.Range(-ShakeIntensity, ShakeIntensity)*.2f,
                                   OriginalRot.w + Random.Range(-ShakeIntensity, ShakeIntensity)*.2f);
 
        ShakeIntensity -= ShakeDecay;
     }
     else if (Shaking)
     {
        Shaking = false;  
     }
 }
     
     
 void OnGUI() {
 
     if (GUI.Button(new Rect(10, 200, 50, 30), "Shake"))
         DoShake();
         Debug.Log("Shake");
 
 }        
         
 
         
         
     
 
 public void DoShake()
 {
     OriginalPos = transform.position;
     OriginalRot = transform.rotation;
 
     ShakeIntensity = 0.3f;
     ShakeDecay = 0.02f;
     Shaking = true;
 }    
     
     
 }
 

EDIT: mdjasper, thanks for coming up with the original solution!

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
avatar image
0

Answer by bgprocks · Jan 11, 2013 at 11:47 PM

 using UnityEngine;
 using System.Collections;
 
 public class ShakeCamera : MonoBehaviour 
 {
 public bool Shaking;
 private float ShakeDecay;
 private float ShakeIntensity;
 
 private Vector3 OriginalPos;
 private Quaternion OriginalRot;
 
 void Start()
 {
     Shaking = false;    
 }
 
 
 // Update is called once per frame
 void Update () 
 {
     if(ShakeIntensity > 0)
     {
         transform.position = OriginalPos + Random.insideUnitSphere * ShakeIntensity;
         transform.rotation = new Quaternion(OriginalRot.x + Random.Range(-ShakeIntensity, ShakeIntensity)*.2f,
                                         OriginalRot.y + Random.Range(-ShakeIntensity, ShakeIntensity)*.2f,
                                         OriginalRot.z + Random.Range(-ShakeIntensity, ShakeIntensity)*.2f,
                                         OriginalRot.w + Random.Range(-ShakeIntensity, ShakeIntensity)*.2f);
         
         ShakeIntensity -= ShakeDecay;
     }
     else if (Shaking)
     {
         Shaking = false;    
     }
 }
 
 public void DoShake()
 {
     OriginalPos = transform.position;
     OriginalRot = transform.rotation;
     
     ShakeIntensity = 0.3f;
     ShakeDecay = 0.02f;
     Shaking = true;
 }

}

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