PlayerDamageReceiver.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. /// <summary>
  5. /// Handles fading in/out the material of the object it's attached to for an effect when the player takes damage.
  6. /// in the ZED Drone Battle sample, this makes a sphere around the player's head turn red and fade out over a second. Also plays a sound.
  7. /// Used by LaserShot_Drone to know when a laser hit the player's head, which then also calls TakeDamage() to make it happen.
  8. /// </summary>
  9. [RequireComponent(typeof(MeshRenderer))]
  10. [RequireComponent(typeof(Collider))]
  11. public class PlayerDamageReceiver : MonoBehaviour
  12. {
  13. /// <summary>
  14. /// How long to display the damage effect.
  15. /// </summary>
  16. [Tooltip("How long to display the damage effect. ")]
  17. public float secondsToDisplayEffect = 1f;
  18. /// <summary>
  19. /// The highest value the damage sphere's material color will be set to
  20. /// </summary>
  21. private float maxcoloralpha;
  22. /// <summary>
  23. /// The current alpha value of the damage sphere's material color.
  24. /// </summary>
  25. private float coloralpha
  26. {
  27. #if !ZED_HDRP && !ZED_URP
  28. get
  29. {
  30. return meshrenderer.material.color.a;
  31. }
  32. set
  33. {
  34. meshrenderer.material.color = new Color(meshrenderer.material.color.r, meshrenderer.material.color.g, meshrenderer.material.color.b, value);
  35. }
  36. #else
  37. get
  38. {
  39. return meshrenderer.material.GetColor("_BaseColor").a;
  40. }
  41. set
  42. {
  43. Color newcol = meshrenderer.material.GetColor("_BaseColor");
  44. newcol.a = value;
  45. meshrenderer.material.SetColor("_BaseColor", newcol);
  46. }
  47. #endif
  48. }
  49. /// <summary>
  50. /// The MeshRenderer attached to this GameObject.
  51. /// </summary>
  52. private MeshRenderer meshrenderer;
  53. /// <summary>
  54. /// The AudioSource attached to this GameObject for playing the hurt sound.
  55. /// </summary>
  56. private AudioSource _audioSource;
  57. // Use this for initialization
  58. void Start()
  59. {
  60. meshrenderer = GetComponent<MeshRenderer>();
  61. maxcoloralpha = meshrenderer.material.color.a;
  62. //Set the alpha to zero as we haven't taken damage yet.
  63. coloralpha = 0f;
  64. _audioSource = GetComponent<AudioSource>();
  65. }
  66. // Update is called once per frame
  67. void Update()
  68. {
  69. //Tick down the color if it's above zero.
  70. if(coloralpha > 0f)
  71. {
  72. /*float newalpha = coloralpha - Time.deltaTime / secondsToDisplayEffect * maxcoloralpha;
  73. if (newalpha < 0f) newalpha = 0f;
  74. coloralpha = newalpha;*/
  75. coloralpha -= Time.deltaTime / secondsToDisplayEffect * maxcoloralpha;
  76. }
  77. }
  78. /// <summary>
  79. /// Causes the damage effect to play once.
  80. /// </summary>
  81. public void TakeDamage()
  82. {
  83. coloralpha = maxcoloralpha; //Set the damage sphere to as high as we want it to ever get
  84. if(_audioSource)
  85. {
  86. _audioSource.Play(); //Play the "ouch" sound.
  87. }
  88. }
  89. }