LaserShot_Player.cs 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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 deals damage to objects with ILaserable in their root gameobject.
  6. /// Inherits from Projectile.cs, where most logic not specific to the ZED Drone Battle sample is handled.
  7. /// </summary>
  8. public class LaserShot_Player : ZEDProjectile
  9. {
  10. /// <summary>
  11. /// How much damage we do to a drone, or another object that inherits from ILaserable.
  12. /// </summary>
  13. [Tooltip("How much damage we do to a drone, or another object that inherits from ILaserable.")]
  14. public int Damage = 10;
  15. /// <summary>
  16. /// Explosion object to spawn on death.
  17. /// </summary>
  18. [Tooltip("Explosion object to spawn on death.")]
  19. public GameObject explosionPrefab;
  20. /// <summary>
  21. /// Called when the projectile hits the real world.
  22. /// As the real world can't be a distinct gameobject (for now) we just spawn FX and destroy the projectile.
  23. /// </summary>
  24. /// <param name="collisionpoint"></param>
  25. /// <param name="collisionnormal"></param>
  26. public override void OnHitRealWorld(Vector3 collisionpoint, Vector3 collisionnormal)
  27. {
  28. SpawnExplosion(collisionpoint, collisionnormal);
  29. Destroy(gameObject);
  30. }
  31. /// <summary>
  32. /// Called when the projectile hits a virtual object.
  33. /// If the object has a component that implements ILaserable, deal damage and destroy the projectile.
  34. /// Otherwise ignore it so the player can't shoot themselves.
  35. /// </summary>
  36. /// <param name="hitinfo"></param>
  37. public override void OnHitVirtualWorld(RaycastHit hitinfo)
  38. {
  39. //Deal damage to an object if it contains ILaserable, such as a drone
  40. ILaserable laserable = hitinfo.collider.transform.root.GetComponent<ILaserable>();
  41. if (laserable != null) //We don't want any collision logic if it doesn't have ILaserable, so we can avoid the player shooting themselves in the head.
  42. {
  43. //Inflict damage on the object we hit
  44. laserable.TakeDamage(Damage);
  45. //Create the explosion
  46. SpawnExplosion(hitinfo.point, hitinfo.normal);
  47. Destroy(gameObject);
  48. }
  49. }
  50. /// <summary>
  51. /// Creates an explosion effect at the desired location, used when the laser is destroyed.
  52. /// </summary>
  53. /// <param name="position">Where to spawn the explosion.</param>
  54. /// <param name="normal">Which direction to orient the explosion. Should be based off the surface normal of the object the projectile hit.</param>
  55. void SpawnExplosion(Vector3 position, Vector3 normal)
  56. {
  57. if (explosionPrefab) //Only spawn an explosion if we have a prefab to spawn
  58. {
  59. GameObject explosion = Instantiate(explosionPrefab);
  60. explosion.transform.position = position;
  61. explosion.transform.rotation = Quaternion.LookRotation(normal);
  62. }
  63. }
  64. }