Advertisement
Guest User

Untitled

a guest
Mar 27th, 2015
23
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.27 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class CameraShaker : MonoBehaviour
  5. {
  6.     public static void AddShake(float amount)
  7.     {
  8.         shake += amount;
  9.     }
  10.  
  11.     static float shake = 0;
  12.     public static float Shake
  13.     {
  14.         get { return CameraShaker.shake; }
  15.         set { CameraShaker.shake = value; }
  16.     }
  17.  
  18.     float MaxShake = 10;
  19.     float LinearShakeAmount = 0.066f;
  20.     float ShakeStrength = 10;
  21.     float lerpAmount = 1;
  22.     float shakeBleed = 4;
  23.  
  24.     Vector3 restingPosition;
  25.  
  26.     void Start()
  27.     {
  28.         restingPosition = transform.position;        
  29.     }
  30.  
  31.     void Update ()
  32.     {
  33.         Shake -= Time.deltaTime * shakeBleed;
  34.         Shake = Mathf.Max(Shake, 0);
  35.         Shake = Mathf.Min(Shake, MaxShake);
  36.         Vector3 newPos = transform.position;
  37.         float applyShake = Mathf.Log(Shake) + Shake * LinearShakeAmount;
  38.         if (Shake > 0)
  39.         {
  40.             newPos += new Vector3(
  41.                 Random.Range(-applyShake * ShakeStrength, applyShake * ShakeStrength),
  42.                 Random.Range(-applyShake * ShakeStrength, applyShake * ShakeStrength),
  43.                 0);
  44.         }
  45.         transform.position = Vector3.Lerp(restingPosition, newPos,
  46.             Time.deltaTime * Shake * lerpAmount);
  47.     }
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement