Advertisement
duck

Generic Damage Component

Nov 12th, 2011
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.76 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class Damage : MonoBehaviour {
  5.    
  6.     public float currentShield;
  7.     public float maxShield = 1;
  8.     public Explosion.ExplosionType explosionType = Explosion.ExplosionType.Medium;
  9.     [HideInInspector] public float lastExplosionDamage;
  10.     [HideInInspector] public Vector3 lastVelocity;
  11.     [HideInInspector] public float factor;
  12.     public float slideDamage = 1;
  13.     public DieType dieType = DieType.Destroy;
  14.    
  15.     public enum DieType {
  16.         None,
  17.         Unfreeze,
  18.         Destroy
  19.     }
  20.    
  21.     public void Start()
  22.     {
  23.         currentShield = maxShield;
  24.         lastExplosionDamage = currentShield;
  25.     }
  26.    
  27.     void FixedUpdate()
  28.     {
  29.         if (rigidbody != null) {
  30.             lastVelocity = rigidbody.velocity;
  31.         }
  32.     }
  33.    
  34.    
  35.     public void Add(float amount, Vector3 pos)
  36.     {
  37.         if (currentShield > 0) {
  38.             currentShield = Mathf.Clamp(currentShield - amount, 0, maxShield);
  39.            
  40.             if (currentShield == 0 )
  41.             {
  42.                 Explode();
  43.             } else {
  44.                
  45.                
  46.                 // small explosions for gradual damage
  47.                 if (currentShield < lastExplosionDamage - 0.1f) {
  48.                     if (amount >= 0.3f) {
  49.                         Explosion.Explode(pos,lastVelocity,Explosion.ExplosionType.Medium);
  50.                     } else if (amount >= 0.05f) {
  51.                         Explosion.Explode(pos,lastVelocity,Explosion.ExplosionType.Small);
  52.                     }
  53.                     lastExplosionDamage = currentShield;
  54.                 }
  55.                
  56.                 factor = Mathf.InverseLerp(maxShield,0,currentShield);
  57.                
  58.             }
  59.         }
  60.        
  61.     }
  62.    
  63.     public void Explode()
  64.     {
  65.         Explosion e = Explosion.Explode(transform.position,lastVelocity,explosionType);
  66.        
  67.         Collider[] myColliders = GetComponentsInChildren<Collider>();
  68.        
  69.         foreach (Collider c in e.GetComponentsInChildren<Collider>())
  70.         {
  71.             foreach (Collider c2 in myColliders) {
  72.                 Physics.IgnoreCollision(c,c2);         
  73.             }
  74.         }
  75.        
  76.         switch (dieType) {
  77.             case DieType.Destroy: Destroy(gameObject); break;
  78.             case DieType.Unfreeze: rigidbody.constraints = RigidbodyConstraints.None; break;
  79.         }  
  80.        
  81.         CameraMovement mainCam = Camera.main.GetComponent<CameraMovement>();
  82.        
  83.         // if camera was watching this object, now watch the explosion instead
  84.         if (mainCam.target == transform)
  85.         {
  86.             Time.timeScale = 0.1f;
  87.             mainCam.target = e.transform;
  88.         }
  89.        
  90.        
  91.     }
  92.    
  93.    
  94.     void OnCollisionEnter(Collision c)
  95.     {
  96.         if (rigidbody != null)
  97.         {
  98.             float velocityChangeFactor = Mathf.InverseLerp(0,90,Vector3.Angle(rigidbody.velocity,lastVelocity));
  99.             Add( c.impactForceSum.magnitude * 0.01f * velocityChangeFactor, c.contacts[0].point);
  100.         } else {
  101.             // this is a static object - be damaged by impact of other moving object
  102.             Add( c.impactForceSum.magnitude * 0.01f, c.contacts[0].point);
  103.         }
  104.        
  105.     }
  106.    
  107.     void OnCollisionStay(Collision c)
  108.     {
  109.         //Debug.Log(slideDamage);
  110.         Add( c.impactForceSum.magnitude * 0.00001f * slideDamage, c.contacts[0].point);
  111.        
  112.     }
  113.    
  114.    
  115.    
  116. }
  117.  
  118.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement