Advertisement
Guest User

Untitled

a guest
Mar 26th, 2013
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.90 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class ShakeCameraOnCollisionWithTags : MonoBehaviour {
  5.    
  6.     public float shakeMagnitude = 1.0f;
  7.     public float shakeTime = 1.0f;
  8.    
  9.     public string[] collideWithTags;
  10.    
  11.     private float shakeTimer = 0;
  12.     private Vector3 originalPosition;
  13.    
  14.     // Update is called once per frame
  15.     void Update ()
  16.     {
  17.         if(shakeTimer > 0)
  18.         {
  19.             Vector3 newPosition = originalPosition;
  20.             newPosition += Random.insideUnitSphere*shakeMagnitude;
  21.             Camera.main.transform.position = newPosition;
  22.             shakeTimer-=Time.deltaTime;
  23.             if(shakeTimer < 0)
  24.             {
  25.                 Camera.main.transform.position = originalPosition;
  26.             }
  27.         }
  28.     }
  29.    
  30.     void OnCollisionEnter( Collision collision )
  31.     {
  32.         foreach(string tag in collideWithTags)
  33.         {
  34.             if(collision.collider.tag == tag)
  35.             {
  36.                
  37.                 shakeTimer = shakeTime;
  38.                 originalPosition = Camera.main.transform.position;
  39.             }
  40.         }
  41.     }
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement