GibTreaty

ShakeyCamera.cs

Aug 7th, 2015
250
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.79 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class ShakeyCamera : MonoBehaviour {
  5.    
  6.     public float recoverStrength = 1;
  7.     public AnimationCurve shakeCurve = new AnimationCurve(new Keyframe[]{ new Keyframe(0,0), new Keyframe(1,1)});
  8.     public Transform shakePivot;
  9.    
  10.     Vector3 startPosition;
  11.     Vector3 offset;
  12.     float velocity;
  13.    
  14.     void Awake(){
  15.         startPosition = shakePivot.localPosition;
  16.     }
  17.    
  18.     void FixedUpdate(){
  19.         velocity -= velocity * Time.deltaTime * recoverStrength;
  20.        
  21.         if(velocity > 0)
  22.             offset = Random.insideUnitSphere * shakeCurve.Evaluate(velocity) * velocity;
  23.  
  24.         shakePivot.localPosition = startPosition + offset;
  25.     }
  26.    
  27.     public void Shake(float force){
  28.         velocity += force;
  29.     }
  30.    
  31.     [ContextMenu("Shake")]
  32.     void ShakeTest(){
  33.         Shake(Random.value * 10);
  34.     }
  35. }
Advertisement
Add Comment
Please, Sign In to add comment