ZEDProjectile.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.Networking;
  5. /// <summary>
  6. /// Checks for collisions with both the real and virtual world and moves forward each frame.
  7. /// Inherit from this class to easily make your own projectile that can hit both the virtual and real.
  8. /// See LaserShot_Drone and LaserShot_Player in the ZED Drone Battle sample for examples.
  9. /// Real world "collisions" test if it's behind the real world using the static hit test functions in ZEDSupportFunctions.
  10. /// </summary>
  11. public class ZEDProjectile : MonoBehaviour
  12. {
  13. /// <summary>
  14. /// How fast the projectile moves forward in meters per second.
  15. /// </summary>
  16. [Tooltip("How fast the projectile moves forward in meters per second.")]
  17. public float speed = 10f;
  18. /// <summary>
  19. /// How long the projectile lasts before being destroyed, even if it doesn't collide with anything.
  20. /// </summary>
  21. [Tooltip("How long the projectile lasts before being destroyed, even if it doesn't collide with anything.")]
  22. public float lifespan = 8f;
  23. /// <summary>
  24. /// How granular the dots are along the fake raycast done for real-world collisions.
  25. /// </summary>
  26. [Tooltip("How granular the dots are along the fake raycast done for real-world collisions.")]
  27. public float distanceBetweenRayChecks = 0.01f;
  28. /// <summary>
  29. /// How far behind a real pixel can a collision check decide it's not a collision.
  30. /// </summary>
  31. [Tooltip("How far behind a real pixel can a collision check decide it's not a collision.")]
  32. public float realWorldThickness = 0.05f;
  33. /// <summary>
  34. /// If true, all active ZEDs are used to check for collisions.
  35. /// <para>This can mean being able to shoot things that aren't visible to the first player, but due to the
  36. /// nature of collisions testing if virtual objects are behind the real world, can lead to
  37. /// false positives if, for instance, a player in pass-through AR is between a projectile and a third-person ZED.
  38. /// Use the realWorldThickness value to help mitigate this.</para>
  39. /// </summary>
  40. [Tooltip("If true, all active ZEDs are used to check for collisions. " +
  41. "This can mean being able to shoot things that aren't visible to the first player, but due to the " +
  42. "nature of collisions testing if virtual objects are behind the real world, can lead to " +
  43. "false positives if, for instance, a player in pass-through AR is between a projectile and a third-person ZED. " +
  44. "Use the realWorldThickness value to help mitigate this.")]
  45. public bool testCollisionsUsingAllZEDs = true;
  46. /// <summary>
  47. /// Primary ZEDManager, used for collision detection when testCollisionsUsingAllZEDs is set to false.
  48. /// </summary>
  49. public ZEDManager zedManager = null;
  50. /// <summary>
  51. /// The ZED camera reference, used for calling ZEDSupportFunctions to transform depth data from the ZED into world space.
  52. /// </summary>
  53. protected Camera cam;
  54. private void Awake()
  55. {
  56. if (zedManager == null)
  57. {
  58. zedManager = FindObjectOfType<ZEDManager>();
  59. //If this happenend when only using a primary ZED for collisions but there are multiple ZEDs, collisions will be
  60. //calculated using an arbitrary camera. Warn the user.
  61. if (testCollisionsUsingAllZEDs == false && ZEDManager.GetInstances().Count > 1)
  62. {
  63. Debug.LogWarning("Warning: ZEDProjectile's zedManager value was not specified, resulting in assigning to first available " +
  64. " camera, but there are multiple cameras in the scene. This can cause strange collision test behavior.");
  65. }
  66. }
  67. if (!cam)
  68. {
  69. cam = zedManager.GetMainCamera();
  70. }
  71. }
  72. /// <summary>
  73. /// Handles movements and collisions on a constant basis.
  74. /// </summary>
  75. void FixedUpdate()
  76. {
  77. //Calculate where the object should move this frame
  78. Vector3 newpos = transform.position + transform.rotation * Vector3.forward * (speed * Time.deltaTime);
  79. //Collisions with the real World. As the object moves, collisions checks are made each frame at the next position.
  80. Vector3 collisionpoint;
  81. Vector3 collisionnormal;
  82. //First, test the primary ZED. Collisions will look the most accurate if calculated from this one.
  83. bool primaryhit = ZEDSupportFunctions.HitTestOnRay(zedManager.zedCamera, cam, newpos, transform.rotation, Vector3.Distance(transform.position, newpos),
  84. distanceBetweenRayChecks, out collisionpoint, false, realWorldThickness);
  85. if (primaryhit)
  86. {
  87. //Call the function to resolve the impact. This allows us to easily override what happens on collisions with classes that inherit this one.
  88. ZEDSupportFunctions.GetNormalAtWorldLocation(zedManager.zedCamera, collisionpoint, sl.REFERENCE_FRAME.WORLD, cam, out collisionnormal);
  89. OnHitRealWorld(collisionpoint, collisionnormal);
  90. }
  91. if (!primaryhit && testCollisionsUsingAllZEDs) //If set to true, test the rest of the ZEDs as well.
  92. {
  93. foreach (ZEDManager manager in ZEDManager.GetInstances())
  94. {
  95. if (manager == zedManager) continue; //If it's the primary ZED, skip as we've already tested that one.
  96. if (ZEDSupportFunctions.HitTestOnRay(manager.zedCamera, manager.GetMainCamera(), newpos, transform.rotation, Vector3.Distance(transform.position, newpos), distanceBetweenRayChecks, out collisionpoint, false, realWorldThickness))
  97. {
  98. //Call the function to resolve the impact. This allows us to easily override what happens on collisions with classes that inherit this one.
  99. ZEDSupportFunctions.GetNormalAtWorldLocation(manager.zedCamera, collisionpoint, sl.REFERENCE_FRAME.WORLD, manager.GetMainCamera(), out collisionnormal);
  100. OnHitRealWorld(collisionpoint, collisionnormal);
  101. break; //No need to test the rest of the ZEDs.
  102. }
  103. }
  104. }
  105. //Collisions with virtual objects
  106. //Cast a ray to check collisions between here and the intended move point for virtual objects.
  107. RaycastHit hitinfo;
  108. if (Physics.Raycast(transform.position, newpos - transform.position, out hitinfo, Vector3.Distance(transform.position, newpos)))
  109. {
  110. //Call the function to resolve the impact. This allows us to easily override what happens on collisions with classes that inherit this one.
  111. OnHitVirtualWorld(hitinfo);
  112. }
  113. //Move it to this new place
  114. transform.position = newpos;
  115. //Tick down its lifespan and check if we should destroy it.
  116. lifespan -= Time.deltaTime;
  117. if (lifespan <= 0f)
  118. {
  119. Destroy(gameObject);
  120. }
  121. }
  122. /// <summary>
  123. /// Called when the projectile hits real geometry. Override to spawn effects, play sounds, etc.
  124. /// </summary>
  125. /// <param name="collisionpoint"></param>
  126. /// <param name="collisionnormal"></param>
  127. public virtual void OnHitRealWorld(Vector3 collisionpoint, Vector3 collisionnormal)
  128. {
  129. Destroy(gameObject);
  130. }
  131. /// <summary>
  132. /// Called when the projectile hits a virtual collider. Override to spawn effects, inflict damage, etc.
  133. /// </summary>
  134. /// <param name="hitinfo">The RayCastHit supplied by the raycast used to detect the collision.</param>
  135. public virtual void OnHitVirtualWorld(RaycastHit hitinfo)
  136. {
  137. Destroy(gameObject);
  138. }
  139. }