1
0

ArcheryTarget.cs 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. //======= Copyright (c) Valve Corporation, All rights reserved. ===============
  2. //
  3. // Purpose: Target that sends events when hit by an arrow
  4. //
  5. //=============================================================================
  6. using UnityEngine;
  7. using UnityEngine.Events;
  8. using System.Collections;
  9. namespace Valve.VR.InteractionSystem
  10. {
  11. //-------------------------------------------------------------------------
  12. public class ArcheryTarget : MonoBehaviour
  13. {
  14. public UnityEvent onTakeDamage;
  15. public bool onceOnly = false;
  16. public Transform targetCenter;
  17. public Transform baseTransform;
  18. public Transform fallenDownTransform;
  19. public float fallTime = 0.5f;
  20. const float targetRadius = 0.25f;
  21. private bool targetEnabled = true;
  22. //-------------------------------------------------
  23. private void ApplyDamage()
  24. {
  25. OnDamageTaken();
  26. }
  27. //-------------------------------------------------
  28. private void FireExposure()
  29. {
  30. OnDamageTaken();
  31. }
  32. //-------------------------------------------------
  33. private void OnDamageTaken()
  34. {
  35. if ( targetEnabled )
  36. {
  37. onTakeDamage.Invoke();
  38. StartCoroutine( this.FallDown() );
  39. if ( onceOnly )
  40. {
  41. targetEnabled = false;
  42. }
  43. }
  44. }
  45. //-------------------------------------------------
  46. private IEnumerator FallDown()
  47. {
  48. if ( baseTransform )
  49. {
  50. Quaternion startingRot = baseTransform.rotation;
  51. float startTime = Time.time;
  52. float rotLerp = 0f;
  53. while ( rotLerp < 1 )
  54. {
  55. rotLerp = Util.RemapNumberClamped( Time.time, startTime, startTime + fallTime, 0f, 1f );
  56. baseTransform.rotation = Quaternion.Lerp( startingRot, fallenDownTransform.rotation, rotLerp );
  57. yield return null;
  58. }
  59. }
  60. yield return null;
  61. }
  62. }
  63. }