Bunny.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. /// <summary>
  5. /// Governs the FlyingBunny prefab used in the ZED plane detection samples.
  6. /// Checks for collisions in front of it in midair, and spawns the flag when it hits the ground.
  7. /// </summary>
  8. public class Bunny : MonoBehaviour
  9. {
  10. /// <summary>
  11. /// Stores initial position when we are enabled/spawned into the scene.
  12. /// Used to calculate how far the bunny traveled after we hit it in the VR plane detection demo.
  13. /// </summary>
  14. public Vector3 InitialPosition { get; private set; }
  15. /// <summary>
  16. /// True whenever this object is moved by the baseball bat.
  17. /// </summary>
  18. public bool IsMoving { get; private set; }
  19. /// <summary>
  20. /// Reference to the BunnySpawner that spawned this GameObject.
  21. /// </summary>
  22. private BunnySpawner bunnyspawner;
  23. /// <summary>
  24. /// The transform used as center/pivot point for this GameObject when checking for collisions with the real world.
  25. /// </summary>
  26. private Transform centerpoint;
  27. /// <summary>
  28. /// Reference to the scene's ZED Plane Detection Manager.
  29. /// </summary>
  30. private ZEDPlaneDetectionManager planeManager;
  31. /// <summary>
  32. /// The bunny's Rigidbody component.
  33. /// </summary>
  34. private Rigidbody rb;
  35. /// <summary>
  36. /// The Bunny's Animator component.
  37. /// </summary>
  38. [HideInInspector]
  39. public Animator anim;
  40. // Use this for initialization.
  41. void Start()
  42. {
  43. IsMoving = false; //we're not moving at start.
  44. //Caching
  45. planeManager = FindObjectOfType<ZEDPlaneDetectionManager>();
  46. rb = GetComponent<Rigidbody>(); //Get our Rigidbody component.
  47. anim = GetComponent<Animator>();
  48. InitialPosition = transform.position; //Save for calculating distance traveled later.
  49. //If there is a child in position 2, use it as new centerPoint.
  50. if (transform.GetChild(2) != null)
  51. {
  52. centerpoint = transform.GetChild(2);
  53. }
  54. else //If not, use this transform.
  55. {
  56. centerpoint = transform;
  57. }
  58. }
  59. /// <summary>
  60. /// Sets the BunnySpawner.
  61. /// </summary>
  62. /// <param name="spawner"></param>
  63. public void SetMySpawner(BunnySpawner spawner)
  64. {
  65. bunnyspawner = spawner;
  66. }
  67. /// <summary>
  68. /// Starts the HitDelay coroutine.
  69. /// </summary>
  70. public void GetHit(bool hit)
  71. {
  72. GetComponent<Rigidbody>().drag = 0f;
  73. GetComponent<Rigidbody>().angularDrag = 0.5f;
  74. StartCoroutine(HitDelay(hit));
  75. }
  76. /// <summary>
  77. /// Coroutine used to delay the collision detection of the Bunny.
  78. /// Setting the _moving variable after waiting X seconds.
  79. /// </summary>
  80. IEnumerator HitDelay(bool hit)
  81. {
  82. //Wait for X amount of seconds...
  83. yield return new WaitForSeconds(0.1f);
  84. if (hit)
  85. {
  86. //... then set IsMoving to true, and allow collision detection in FixedUpdate().
  87. IsMoving = true;
  88. }
  89. else
  90. {
  91. rb.isKinematic = true; //Freeze the object at the current position.
  92. yield return new WaitForSeconds(1f);
  93. bunnyspawner.SpawnUI(transform.position);
  94. }
  95. //Clearing the scene from any Planes created by the ZED Plane Detection Manager.
  96. for (int i = 0; i < planeManager.hitPlaneList.Count; i++)
  97. {
  98. Destroy(planeManager.hitPlaneList[i].gameObject);
  99. planeManager.hitPlaneList.RemoveAt(i);
  100. }
  101. }
  102. /// <summary>
  103. /// This function is called every fixed framerate frame
  104. /// Here we take care of enabling & disabling the laser pointer.
  105. /// </summary>
  106. private void FixedUpdate()
  107. {
  108. //If we have been moved by the baseball bat
  109. if (IsMoving)
  110. {
  111. //Look for our next position based on our current velocity.
  112. Vector3 predictedPos = centerpoint.position + (rb.velocity * (Time.deltaTime * 2.5f));
  113. transform.rotation = Quaternion.LookRotation(rb.velocity.normalized);
  114. //Collision check with the real world at that next position.
  115. foreach (ZEDManager manager in ZEDManager.GetInstances()) //Check all active cameras.
  116. {
  117. if (ZEDSupportFunctions.HitTestAtPoint(manager.zedCamera, manager.GetMainCamera(), predictedPos))
  118. {
  119. //We hit something, but is it a flat surface?
  120. if (planeManager.DetectPlaneAtHit(manager, manager.GetMainCamera().WorldToScreenPoint(predictedPos)))
  121. {
  122. bunnyspawner.SpawnUI(predictedPos);
  123. IsMoving = false;
  124. }
  125. else//If not, bounce off of it but still show the flag.
  126. {
  127. IsMoving = false; //Not moving anymore, so update our state.
  128. bunnyspawner.SpawnUI(predictedPos); //Start spawning the UI on our current location.
  129. rb.velocity = Vector3.Reflect(rb.velocity / 2, transform.forward); //Bounce off the surface we hit
  130. }
  131. break; //If it hit the real world in one camera's view, no need to check the other cameras.
  132. }
  133. }
  134. }
  135. }
  136. }