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
1
Question by Hamesh81 · Feb 14, 2014 at 11:02 AM · camerarandomsmoothshake

How can I make a camera shake more smoothly (Random.insideUnitSphere)?

I'm working on a pretty standard camera shake script which uses Random.insideUnitSphere to set the camera's position for the "shaking". I would like to be able to control the speed in which the camera moves from one random position to the next, therefore controlling how smoothly it shakes. At the moment I can control the shaking intensity and the length it shakes for, but I'd like to control the shaking speed/smoothness because at the moment the movement is very jerky and sometimes this is not what's needed. I've tried using a vector3.lerp with a speed variable but this doesn't seem to add any smoothness. Any suggestions how this could be done? Here is my script:

 using UnityEngine;
 using System.Collections;
 
 public class CameraShake : MonoBehaviour
 {
     public Transform cameraTransform;
     public float shakeLength = 2;
     public float shakeTimer;
     public float shakeAmount = 0.7f;
     public float shakeSpeed = 0.7f;
     public bool isShaking = false;
     public bool shakeOnce = false;
     Vector3 originalPos;
     
     void Awake()
     {
         shakeTimer = shakeLength;
     }
     
     void OnEnable()
     {
         originalPos = cameraTransform.localPosition;
     }
     
     void Update()
     {
         if (Input.GetKeyDown(KeyCode.Return) && !isShaking) {
             shakeOnce = true;
             shakeTimer = shakeLength;
         }
 
         if (shakeOnce) {
             Shake ();
         }
     }
 
     public void Shake() {
         if (shakeTimer > 0)
         {
             isShaking = true;
             cameraTransform.localPosition = Vector3.Lerp(cameraTransform.localPosition, originalPos + Random.insideUnitSphere * shakeAmount, shakeSpeed);
 
             shakeTimer -= Time.deltaTime;
         }
         else 
         {
             shakeTimer = 0f;
             cameraTransform.localPosition = originalPos;
             isShaking = false;
             shakeOnce = false;
         }
     }
 }
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

1 Reply

· Add your reply
  • Sort: 
avatar image
3
Best Answer

Answer by nasa8 · Feb 14, 2014 at 11:44 AM

You change the new position for the camera each frame and your shake speed is too high for lerp (the camera reaches new positions during two frames) because t-parameter is maximum 1 and you have 0.7 increment.

try the code:

 using UnityEngine;
 using System.Collections;
 
 public class NewBehaviourScript1 : MonoBehaviour {
 
 
         public Transform cameraTransform;
         public float shakeLength = 5;
         public float shakeTimer;
         public float shakeAmount = 3;
         public float shakeSpeed = 20;
         public bool isShaking = false;
         public bool shakeOnce = false;
         Vector3 originalPos;
     Vector3 newPos;
 
         void Awake()
         {
             shakeTimer = shakeLength;
         }
         
         void OnEnable()
         {
             originalPos = cameraTransform.position;
         }
         
         void Update()
         {
             if (Input.GetKeyDown(KeyCode.Return) && !isShaking) {
                 shakeOnce = true;
                 shakeTimer = shakeLength;
                 newPos = cameraTransform.position;
             }
             
             if (shakeOnce) {
                 Shake ();
             }
         }
         
         public void Shake() {
             if (shakeTimer > 0)
             {
                 isShaking = true;
 
             if (Vector3.Distance(newPos,cameraTransform.position)<=shakeAmount/30) {newPos = originalPos+Random.insideUnitSphere * shakeAmount;}
 
             cameraTransform.position = Vector3.Lerp(cameraTransform.position, newPos , Time.deltaTime*shakeSpeed);
                 
                 shakeTimer -= Time.deltaTime;
             }
             else 
             {
                 shakeTimer = 0f;
                 cameraTransform.position = originalPos;
                 isShaking = false;
                 shakeOnce = false;
             }
         }
     }
Comment
Add comment · Show 2 · 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 Hamesh81 · Feb 14, 2014 at 12:10 PM 0
Share

Champion! That's exactly it. Thanks so much

avatar image darshil12bharuhcha · Jan 12, 2021 at 04:27 PM 0
Share

but i have a first person camera and when the starts shaking it do not follow head of player why?

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

20 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

Related Questions

Smooth dizzy camera shake. 0 Answers

Constant camera shake. 1 Answer

iTween MoveTo Function is Jumpy 3 Answers

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

Camera, Look rotation, smoothly? 0 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