webik150

Shield.cs

Jan 17th, 2016
377
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.30 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using UnityStandardAssets.Utility;
  4.  
  5. public class Shield : MonoBehaviour
  6. {
  7.     //Color of the shield (affects only illumination. Not sprite color)
  8.     public Color shieldColor = Color.white;
  9.     //Light prefab
  10.     public GameObject collisionLight;
  11.  
  12.     //For collisions with other 2D objects
  13.     void OnCollisionEnter2D (Collision2D collision)
  14.     {
  15.         //We want to illuminate all contact points
  16.         for (int i = 0; i < collision.contacts.Length; i++) {
  17.             //For aesthetic purposes we want high-velocity collisions to be brighter than low-velocity collisions. "LightVelocity" determines how many lights we spawn on collision.
  18.             float LightVelocity = 1;
  19.             if (collision.relativeVelocity.magnitude >= 100) {
  20.                 LightVelocity = 5;
  21.             } else if (collision.relativeVelocity.magnitude >= 75) {
  22.                 LightVelocity = 3;
  23.             } else if (collision.relativeVelocity.magnitude >= 40) {
  24.                 LightVelocity = 2;
  25.             } else if (collision.relativeVelocity.magnitude >= 15) {
  26.                 LightVelocity = 1;
  27.             } else {
  28.                 LightVelocity = 0;
  29.             }
  30.             //Now the actual spawning
  31.             for (int j = 0; j < LightVelocity; j++) {
  32.                 //We'll spawn collisionLight prefab, at the point of collision, plus a little bit farther from the shield.
  33.                 Light light = (Instantiate (collisionLight, collision.contacts [i].point + collision.contacts [i].normal, Quaternion.identity)as GameObject).GetComponent<Light> ();
  34.                 light.color = shieldColor;
  35.                 light.type = LightType.Spot;
  36.                 light.bounceIntensity = 0;
  37.                 //values Intensity, Range and Angle depend on your shield size. Test in editor. Make sure the range doesn't go too far behind the shield, as it will illuminate other objects.
  38.                 light.intensity = 10;
  39.                 light.range = 5;
  40.                 light.spotAngle = 75;
  41.                 light.transform.SetParent (transform);
  42.                 //Since the sprite behaves like a sphere, thanks to the normal map, we want to illuminate it at some angle. That's why we have to edit Z position of the light.
  43.                 light.transform.position = new Vector3 (light.transform.position.x, light.transform.position.y, -1);
  44.                 light.transform.LookAt (collision.contacts [i].point);
  45.             }
  46.  
  47.         }
  48.     }
  49.  
  50.     //For manual activation through script (good for weapon hits). Same as above, but only spawns one light.
  51.     public void OnHit (RaycastHit2D hit)
  52.     {
  53.         Light light = (Instantiate (collisionLight, hit.point + (hit.normal * 0.5f), Quaternion.identity)as GameObject).GetComponent<Light> ();
  54.         light.color = shieldColor;
  55.         light.type = LightType.Spot;
  56.         light.bounceIntensity = 0;
  57.         light.intensity = 10;
  58.         light.range = 5;
  59.         light.spotAngle = 75;
  60.         light.transform.SetParent (transform);
  61.         light.transform.LookAt (hit.point);
  62.  
  63.     }
  64.  
  65.     //For collisions with particles
  66.     void OnParticleCollision (GameObject psystem)
  67.     {
  68.         //As noted in the unity documentation, the parameter links to the particle system
  69.         ParticleSystem particleSystem = psystem.GetComponent<ParticleSystem> ();
  70.         ParticleCollisionEvent[] collisions = new ParticleCollisionEvent[particleSystem.GetSafeCollisionEventSize ()];
  71.         //This returns count of collisions between "gameObject" and the particle system and puts all those collisions to "collisions"
  72.         int max = particleSystem.GetCollisionEvents (gameObject, collisions);
  73.         //The rest is same as above.
  74.         for (int i = 0; i < max; i++) {
  75.             float LightVelocityPos = 1;
  76.             if (collisions [i].velocity.magnitude >= 100) {
  77.                 LightVelocityPos = 5;
  78.             } else if (collisions [i].velocity.magnitude >= 75) {
  79.                 LightVelocityPos = 3;
  80.             } else if (collisions [i].velocity.magnitude >= 40) {
  81.                 LightVelocityPos = 2;
  82.             } else if (collisions [i].velocity.magnitude >= 15) {
  83.                 LightVelocityPos = 1;
  84.             } else {
  85.                 LightVelocityPos = 0;
  86.             }
  87.             for (int j = 0; j < LightVelocityPos + 1; j++) {
  88.                 Light light = (Instantiate (collisionLight, collisions [i].intersection + collisions [i].normal, Quaternion.LookRotation (collisions [i].normal))as GameObject).GetComponent<Light> ();
  89.                 light.color = shieldColor;
  90.                 light.type = LightType.Spot;
  91.                 light.bounceIntensity = 0;
  92.                 light.intensity = 10;
  93.                 light.range = 5;
  94.                 light.spotAngle = 75;
  95.                 light.transform.SetParent (transform);
  96.                 light.transform.position = new Vector3 (light.transform.position.x, light.transform.position.y, -1);
  97.                 light.transform.LookAt (collisions [i].intersection);
  98.             }
  99.         }
  100.     }
  101. }
Advertisement
Add Comment
Please, Sign In to add comment