- Home /
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;
         }
     }
 }
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;
             }
         }
     }
but i have a first person camera and when the starts shaking it do not follow head of player why?
Your answer
 
 
             Follow this Question
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
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                