ArUcoDroneLaser.cs 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. /// <summary>
  5. /// Laser spawned from an AruCoDrone. Should be in a prefab given to ArUcoDrone.projectilePrefab.
  6. /// See parent class ZEDProjectile to see how projectile collision tests (real and virtual) are performed.
  7. /// </summary>
  8. public class ArUcoDroneLaser : ZEDProjectile
  9. {
  10. /// <summary>
  11. /// How much damage is dealt to drones we hit.
  12. /// </summary>
  13. [Tooltip("How much damage is dealt to drones we hit.")]
  14. public float damage;
  15. /// <summary>
  16. /// Which drone spawned this laser. Used to avoid colliding with it.
  17. /// </summary>
  18. [HideInInspector]
  19. public ArUcoDrone spawningDrone;
  20. /// <summary>
  21. /// Object spawned when the laser hits something.
  22. /// </summary>
  23. [Tooltip("Object spawned when the laser hits something.")]
  24. public GameObject explosionPrefab;
  25. /// <summary>
  26. /// Called when the projectile hits a virtual object with a collider.
  27. /// If the object is an ArUco drone, applies damage. Regardless, spawns an explosion and destroys the projectile.
  28. /// </summary>
  29. public override void OnHitVirtualWorld(RaycastHit hitinfo)
  30. {
  31. ArUcoDrone otherdrone = hitinfo.transform.GetComponent<ArUcoDrone>();
  32. if(otherdrone != null && otherdrone != spawningDrone)
  33. {
  34. otherdrone.TakeDamage(damage);
  35. }
  36. if(explosionPrefab)
  37. {
  38. Quaternion explosionrotation = (hitinfo.normal.sqrMagnitude <= 0.001f) ? Quaternion.identity : Quaternion.LookRotation(hitinfo.normal, Vector3.up);
  39. GameObject explosion = Instantiate(explosionPrefab, hitinfo.point, explosionrotation);
  40. }
  41. base.OnHitVirtualWorld(hitinfo); //Destroy.
  42. }
  43. /// <summary>
  44. /// Called when the projectile hits the real world. Creates an explosion and destroys the projectile.
  45. /// </summary>
  46. public override void OnHitRealWorld(Vector3 collisionpoint, Vector3 collisionnormal)
  47. {
  48. if (explosionPrefab)
  49. {
  50. Quaternion explosionrotation = (collisionnormal.sqrMagnitude <= 0.001f) ? Quaternion.identity : Quaternion.LookRotation(collisionnormal, Vector3.up);
  51. GameObject explosion = Instantiate(explosionPrefab, collisionpoint, explosionrotation);
  52. }
  53. base.OnHitRealWorld(collisionpoint, collisionnormal); //Destroy.
  54. }
  55. }