CapsuleFollower.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. using UnityEngine;
  2. /// <summary>
  3. /// Follows a given Capsule object. Used because VR controller movements don't allow for direct physics simulation.
  4. /// Instead, we apply phyics forces by script to make separate capsule colliders follow that controller,
  5. /// which then apply their forces to objects that this object would normally hit.
  6. /// This script is attached to those follower objects. The object being followed has the Capsule component.
  7. /// Used in the ZED VR plane detection sample for the baseball bat.
  8. /// </summary>
  9. public class CapsuleFollower : MonoBehaviour
  10. {
  11. /// <summary>
  12. /// Object this capsule will follow.
  13. /// </summary>
  14. private Capsule target;
  15. /// <summary>
  16. /// Rigidbody attached to this object.
  17. /// </summary>
  18. private Rigidbody rb;
  19. /// <summary>
  20. /// The velocity the rigidbody needs to be set to. Used so we can update the target velocity
  21. /// in Update() but apply to the rigidbody in FixedUpdate() (which is where physics is actually run).
  22. /// </summary>
  23. private Vector3 velocity;
  24. /// <summary>
  25. /// The collider attached to this object.
  26. /// </summary>
  27. private Collider capsulecollider;
  28. /// <summary>
  29. /// Multiplier used to govern how quickly this follower follows its target when it moves.
  30. /// Higher values will make it lag behind less but may cause it to overshoot.
  31. /// </summary>
  32. [SerializeField]
  33. private float sensitivity = 50;
  34. /// <summary>
  35. /// Awake is used to initialize any variables or game states before the game starts.
  36. /// </summary>
  37. private void Awake()
  38. {
  39. rb = GetComponent<Rigidbody>();
  40. capsulecollider = GetComponent<Collider>();
  41. }
  42. /// <summary>
  43. /// Update is called every frame.
  44. /// Here we enable/disable the collider whenever baseball bat is active or not.
  45. /// </summary>
  46. private void Update()
  47. {
  48. if (target.transform.parent.gameObject.activeInHierarchy)
  49. {
  50. capsulecollider.enabled = true;
  51. }
  52. else
  53. capsulecollider.enabled = false;
  54. }
  55. /// <summary>
  56. /// This function is called every fixed framerate frame.
  57. /// Here we calculate the velocity of our rigidbody based on the movement towards the target.
  58. /// </summary>
  59. private void FixedUpdate()
  60. {
  61. Vector3 destination = target.transform.position;
  62. rb.transform.rotation = target.transform.rotation;
  63. velocity = (destination - rb.transform.position) * sensitivity;
  64. rb.velocity = velocity;
  65. }
  66. /// <summary>
  67. /// When another collider enters ours, we assign our rigidbody's velocity to its
  68. /// In the ZED VR plane detection sample, this is how the bunny gets launched. .
  69. /// </summary>
  70. /// <param name="other"></param>
  71. private void OnTriggerEnter(Collider other)
  72. {
  73. Bunny colBunny = other.GetComponent<Bunny>();
  74. //Checking if its a Bunny, with a Rigidbody and that is not moving.
  75. if (colBunny != null)
  76. {
  77. if (other.GetComponent<Rigidbody>() && !colBunny.IsMoving)
  78. {
  79. if (rb.velocity.y <= -2)
  80. {
  81. colBunny.anim.SetTrigger("Squeeze");
  82. colBunny.GetHit(hit: false);
  83. }
  84. else if (rb.velocity.magnitude > 2f)
  85. {
  86. //Send a call to GetHit() which delays for X seconds the Bunny's detection with the real world.
  87. //Since the Bunny is already on the floor, it might return true for collision the moment the baseball bat touches it.
  88. colBunny.GetHit(hit: true);
  89. //Assign our velocity with some changes. Halving the velocity makes it feel more natural when hitting the bunny.
  90. other.GetComponent<Rigidbody>().velocity = rb.velocity / 2;
  91. }
  92. }
  93. }
  94. }
  95. /// <summary>
  96. /// Sets the target to follow. Called by Capsule.
  97. /// </summary>
  98. /// <param name="myTarget"></param>
  99. public void SetFollowTarget(Capsule myTarget)
  100. {
  101. target = myTarget;
  102. }
  103. }