Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 jelenkovic90 · Aug 24, 2016 at 11:13 AM · cameraeffectshake

Camera shake, everything disappear from screen except canvas.

I made camera shake script and when I use it on button everything except canvas disappear from screen (game is continue to go). I make 2d game and in game view option I set 9:14 (I tried with different views and there is same problem). Here is my code:

using UnityEngine; using System.Collections;

public class CameraShake : MonoBehaviour {

 public Camera mainCam;

 float shakeAmount = 0.2f;

 void Awake() {

     if (mainCam == null) {
         mainCam = Camera.main;
     }
 }
 void Update() {

     if (Input.GetKeyDown(KeyCode.T)) {

         Shake(0.1f, 0.1f);
     }
 }
 public void Shake (float amt, float length) {

     shakeAmount = amt;
     InvokeRepeating("BeginShake", 0.01f, 0);
     Invoke("StopShake", length);
 }

 void BeginShake() {

     if (shakeAmount > 0) {

         Vector3 camPos = mainCam.transform.position;

         float offsetX = Random.value * shakeAmount * 2 - shakeAmount;
         float offsetY = Random.value * shakeAmount * 2 - shakeAmount;
         camPos.x += offsetX;
         camPos.y += offsetY;

         mainCam.transform.position = camPos;
     }
 }

 void StopShake()
 {
     CancelInvoke("BeginShake");
     mainCam.transform.position = Vector3.zero;
 }

}

I set Camera as empty game object and MainCamera as child. Script is attached on child object.

Comment
Add comment
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

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Joe_DaBurca · Oct 26, 2017 at 07:34 PM

I have the same problem, can someone tell me if this was resolved?

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 minh23520000 · Feb 15, 2018 at 06:01 AM

In the Function StopShake(), try to change the last line of code into mainCam.transform.localPosition = Vector3.zero; and see what happens. If you change the localPosition into Zero, now the child object will have the same position with the parent object, which should be working. Hope that helps =))

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:16 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

6 People are following this question.

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

Related Questions

Camera Shake Effect (Constant)? 4 Answers

Camera layering with post processing? 0 Answers

Can you use a camera effect that only applies to a certain layer? 1 Answer

camera effect 2 Answers

RenderTexture flickering while multiple RenderTexture rendering 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