EnemyBehavior.cs 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. //======= Copyright (c) Stereolabs Corporation, All rights reserved. ===============
  2. using UnityEngine;
  3. /// <summary>
  4. /// Previously used in the ZED spatial mapping sample scene.
  5. /// Spawns and destroys the bunnies.
  6. /// </summary>
  7. public class EnemyBehavior : MonoBehaviour {
  8. private const float lifeMax = 100;
  9. public float life = lifeMax;
  10. private SphereCollider capsuleCollider;
  11. private bool isDying = false;
  12. private void Start()
  13. {
  14. life = lifeMax;
  15. capsuleCollider = GetComponent<SphereCollider>();
  16. capsuleCollider.enabled = true;
  17. isDying = false;
  18. }
  19. // Update is called once per frame
  20. void Update () {
  21. }
  22. /// <summary>
  23. /// Set the dammage to an object
  24. /// </summary>
  25. /// <param name="value"></param>
  26. public void Dammage(float value)
  27. {
  28. if (!isDying)
  29. {
  30. life -= value;
  31. if (life < 0)
  32. {
  33. Dead();
  34. }
  35. }
  36. }
  37. /// <summary>
  38. /// Disables the gameobject
  39. /// </summary>
  40. public void StartSinking()
  41. {
  42. capsuleCollider.enabled = false;
  43. Destroy(gameObject, 2.0f);
  44. }
  45. /// <summary>
  46. /// Play the animation of dead
  47. /// </summary>
  48. private void Dead()
  49. {
  50. isDying = true;
  51. GetComponent<UnityEngine.AI.NavMeshAgent>().enabled = false;
  52. GetComponent<Animator>().SetTrigger("Dead");
  53. }
  54. private void OnDestroy()
  55. {
  56. isDying = false;
  57. }
  58. }