Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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
0
Question by kp0314 · May 23, 2012 at 10:47 PM · camerascreenshake

Camera Shake

I'm having trouble resetting my camera after using a camera shake script on collision

     public void ScreenShake(bool dead){
   
     if(shakeTimer > 0){
         shakeTimer = 16;
         if(dead)
             shakeTimer = 30;
         Debug.Log("shakeTimer" + shakeTimer);
     }
     else
         StartCoroutine(ScreenShakeRoutine(dead));
 }
 
 public void StopShaking()
 {
     stopShake = true;
 }
 
 public IEnumerator ScreenShakeRoutine(bool dead){
     shakeTimer = 16;
     if(dead)
         shakeTimer = 30;
     Vector3 originalCamPos = myCamera.localPosition;
     while(shakeTimer > 0)
     {
         if (stopShake)
         {
             stopShake = false;
             yield break;
         }
         
         if(shakeTimer % 2 ==0)
             myCamera.localPosition = originalCamPos;
         else
             myCamera.Translate(Random.Range(-0.2f, 0.2f),Random.Range(-0.2f, 0.2f),0);
         shakeTimer--;
         yield return new WaitForSeconds(0.04f);
     }
     myCamera.localPosition = originalCamPos;

 }

When I do not call stopShake() the camera does not return to its original position, but the position but stays at the position of the camera when it originally started shaking.

When I do call stopShake() the camera returns to its original position, but it does not screen shake anymore even if I set screen shake to be false on restart.

Comment
Add comment · Show 1
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 insominx · May 17, 2013 at 08:19 PM 0
Share

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

4 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by insominx · Aug 03, 2013 at 12:52 AM

One clean way to do it is to start maintain the camera's original position, say Vector3 originalPos = ... Then make sure every frame you calculate the shake offset and add it to the original pos. That way, you always know where to come back to. Then to make sure it returns to its starting point, decay the offset value closer and closer to 0 as it gets closer to the time you want to stop the shake. This blog covers it in more detail with some nice visuals: Camera Shake.

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 Loorden · Jun 09, 2017 at 06:43 AM

I'm using this script. Work perfectly for camshake. Not so complicated either.

     bool flip;
 
     float yPos;
     float shakeAmount = 1;
 
     public Transform myCam;
 
     void Update()
     {
         if(Input.GetKeyDown(KeyCode.Space)) StartCoroutine ("CamShake");
     }
 
     IEnumerator CamShake ()
     {
         yPos = shakeAmount;
 
         while (Mathf.Abs(yPos) > 0.01f ) 
         {
             yPos = Mathf.Abs (yPos) - 0.1f;
 
             if (flip) yPos *= -1;
 
             flip = !flip;
 
             //Tripple setup thingo!
             Vector3 tempPos = myCam.localPosition;
             tempPos.y = yPos;
             myCam.localPosition = tempPos;
 
             yield return null;
         }
     }
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 idbrii · Feb 28, 2021 at 06:53 AM

In your code, you could add myCamera.localPosition = originalCamPos before yield break.

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

Answer by thomasfriday · Jul 14, 2021 at 09:18 AM

Here's a short Youtube video that covers exactly how to code a screen shake effect: https://youtu.be/BQGTdRhGmE4


screen shake

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

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.

Follow this Question

Answers Answers and Comments

8 People are following this question.

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

Related Questions

Screen Shake Effect 7 Answers

Is it possible to shake the screen rather than shake the camera? 3 Answers

how to have an object positioned relative to the screen(like a button) 1 Answer

UI render doesn't sync with Camera movement 2 Answers

How to disable shaking camera during pan in editor? 2 Answers


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