LaserShot_Drone.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. /// <summary>
  5. /// A Projectile that spawns an explosion effect on death and causes the player to see a damage effect when hit.
  6. /// Inherits from Projectile.cs, where most logic not specific to the ZED Drone Battle sample is handled.
  7. /// </summary>
  8. public class LaserShot_Drone : ZEDProjectile
  9. {
  10. /// <summary>
  11. /// Explosion object to spawn on death.
  12. /// </summary>
  13. [Tooltip("Explosion object to spawn on death.")]
  14. public GameObject explosionPrefab;
  15. /// <summary>
  16. /// The current distance the object is from the left eye camera, used for collision calculation.
  17. /// </summary>
  18. private float camdistance;
  19. private void Update()
  20. {
  21. camdistance = Vector3.Distance(transform.position, cam.transform.position);
  22. if (camdistance < 1.5f)
  23. speed = 1f;
  24. else
  25. speed = 16f;
  26. }
  27. /// <summary>
  28. /// Called when the projectile hits the real world.
  29. /// As the real world can't be a distinct gameobject (for now) we just spawn FX and destroy the projectile.
  30. /// </summary>
  31. /// <param name="collisionpoint"></param>
  32. /// <param name="collisionnormal"></param>
  33. public override void OnHitRealWorld(Vector3 collisionpoint, Vector3 collisionnormal)
  34. {
  35. SpawnExplosion(collisionpoint, collisionnormal);
  36. Destroy(gameObject);
  37. }
  38. /// <summary>
  39. /// Called when the projectile hits a virtual object.
  40. /// If that object has a PlayerDamageReceiver, deal damage and destroy the projectile.
  41. /// Otherwise ignore it so the drone can't shoot itself.
  42. /// </summary>
  43. /// <param name="hitinfo"></param>
  44. public override void OnHitVirtualWorld(RaycastHit hitinfo)
  45. {
  46. //Check to see if it's the player we hit. If so, deal damage.
  47. PlayerDamageReceiver receiver = hitinfo.collider.gameObject.GetComponent<PlayerDamageReceiver>();
  48. if (receiver != null) //We don't want any collision logic if it's not hitting the player, so we can avoid the drone shooting itself.
  49. {
  50. receiver.TakeDamage();
  51. Destroy(gameObject);
  52. }
  53. }
  54. /// <summary>
  55. /// Creates an explosion effect at the desired location, used when the laser is destroyed.
  56. /// </summary>
  57. /// <param name="position">Where to spawn the explosion.</param>
  58. /// <param name="normal">Which direction to orient the explosion. Should be based off the surface normal of the object the projectile hit.</param>
  59. void SpawnExplosion(Vector3 position, Vector3 normal)
  60. {
  61. if (explosionPrefab) //Only spawn an explosion if we have a prefab to spawn
  62. {
  63. GameObject explosion = Instantiate(explosionPrefab);
  64. explosion.transform.position = position;
  65. explosion.transform.rotation = Quaternion.LookRotation(normal);
  66. }
  67. }
  68. }