JoeJeff.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. using UnityEngine;
  2. using System.Collections;
  3. using Valve.VR.InteractionSystem;
  4. namespace Valve.VR.InteractionSystem.Sample
  5. {
  6. public class JoeJeff : MonoBehaviour
  7. {
  8. public float animationSpeed;
  9. public float jumpVelocity;
  10. [SerializeField]
  11. private float m_MovingTurnSpeed = 360;
  12. [SerializeField]
  13. private float m_StationaryTurnSpeed = 180;
  14. public float airControl;
  15. [Tooltip("The time it takes after landing a jump to slow down")]
  16. public float frictionTime = 0.2f;
  17. [SerializeField]
  18. private float footHeight = 0.1f;
  19. [SerializeField]
  20. private float footRadius = 0.03f;
  21. private RaycastHit footHit;
  22. private bool isGrounded;
  23. private float turnAmount;
  24. private float forwardAmount;
  25. private float groundedTime;
  26. private Animator animator;
  27. private Vector3 input;
  28. private bool held;
  29. private new Rigidbody rigidbody;
  30. private Interactable interactable;
  31. public FireSource fire;
  32. private void Start()
  33. {
  34. animator = GetComponent<Animator>();
  35. rigidbody = GetComponent<Rigidbody>();
  36. interactable = GetComponent<Interactable>();
  37. animator.speed = animationSpeed;
  38. }
  39. private void Update()
  40. {
  41. held = interactable.attachedToHand != null;
  42. jumpTimer -= Time.deltaTime;
  43. CheckGrounded();
  44. rigidbody.freezeRotation = !held;
  45. if (held == false)
  46. FixRotation();
  47. }
  48. private void FixRotation()
  49. {
  50. Vector3 eulers = transform.eulerAngles;
  51. eulers.x = 0;
  52. eulers.z = 0;
  53. Quaternion targetRotation = Quaternion.Euler(eulers);
  54. transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, Time.deltaTime * (isGrounded ? 20 : 3));
  55. }
  56. public void OnAnimatorMove()
  57. {
  58. // we implement this function to override the default root motion.
  59. // this allows us to modify the positional speed before it's applied.
  60. if (Time.deltaTime > 0)
  61. {
  62. Vector3 animationDelta = (animator.deltaPosition) / Time.deltaTime;
  63. animationDelta = Vector3.ProjectOnPlane(animationDelta, footHit.normal);
  64. if (isGrounded && jumpTimer < 0)
  65. {
  66. if (groundedTime < frictionTime) //slow down when first hitting the floor after a jump
  67. {
  68. float moveFac = Mathf.InverseLerp(0, frictionTime, groundedTime);
  69. //print(moveFac);
  70. Vector3 lerpV = Vector3.Lerp(rigidbody.velocity, animationDelta, moveFac * Time.deltaTime * 30);
  71. animationDelta.x = lerpV.x;
  72. animationDelta.z = lerpV.z;
  73. }
  74. // adding a little downward force to keep him on the floor
  75. animationDelta.y += -0.2f;// rb.velocity.y;
  76. rigidbody.velocity = animationDelta;
  77. }
  78. else
  79. {
  80. rigidbody.velocity += input * Time.deltaTime * airControl;
  81. }
  82. }
  83. }
  84. public void Move(Vector3 move, bool jump)
  85. {
  86. input = move;
  87. if (move.magnitude > 1f)
  88. move.Normalize();
  89. move = transform.InverseTransformDirection(move);
  90. turnAmount = Mathf.Atan2(move.x, move.z);
  91. forwardAmount = move.z;
  92. ApplyExtraTurnRotation();
  93. // control and velocity handling is different when grounded and airborne:
  94. if (isGrounded)
  95. {
  96. HandleGroundedMovement(jump);
  97. }
  98. // send input and other state parameters to the animator
  99. UpdateAnimator(move);
  100. }
  101. private void UpdateAnimator(Vector3 move)
  102. {
  103. animator.speed = fire.isBurning ? animationSpeed * 2 : animationSpeed;
  104. // update the animator parameters
  105. animator.SetFloat("Forward", fire.isBurning ? 2 : forwardAmount, 0.1f, Time.deltaTime);
  106. animator.SetFloat("Turn", turnAmount, 0.1f, Time.deltaTime);
  107. animator.SetBool("OnGround", isGrounded);
  108. animator.SetBool("Holding", held);
  109. if (!isGrounded)
  110. {
  111. animator.SetFloat("FallSpeed", Mathf.Abs(rigidbody.velocity.y));
  112. animator.SetFloat("Jump", rigidbody.velocity.y);
  113. }
  114. }
  115. private void ApplyExtraTurnRotation()
  116. {
  117. // help the character turn faster (this is in addition to root rotation in the animation)
  118. float turnSpeed = Mathf.Lerp(m_StationaryTurnSpeed, m_MovingTurnSpeed, forwardAmount);
  119. transform.Rotate(0, turnAmount * turnSpeed * Time.deltaTime, 0);
  120. }
  121. private void CheckGrounded()
  122. {
  123. isGrounded = false;
  124. if (jumpTimer < 0 & !held) // make sure we didn't just jump
  125. {
  126. isGrounded = (Physics.SphereCast(new Ray(transform.position + Vector3.up * footHeight, Vector3.down), footRadius, out footHit, footHeight - footRadius));
  127. if (Vector2.Distance(new Vector2(transform.position.x, transform.position.z), new Vector2(footHit.point.x, footHit.point.z)) > footRadius / 2)
  128. {
  129. isGrounded = false;
  130. //on slope, hit point is on edge of sphere cast
  131. }
  132. }
  133. }
  134. private void FixedUpdate()
  135. {
  136. groundedTime += Time.fixedDeltaTime;
  137. if (!isGrounded) groundedTime = 0; // reset timer
  138. if (isGrounded & !held)
  139. {
  140. Debug.DrawLine(transform.position, footHit.point);
  141. rigidbody.position = new Vector3(rigidbody.position.x, footHit.point.y, rigidbody.position.z);
  142. }
  143. }
  144. private void HandleGroundedMovement(bool jump)
  145. {
  146. // check whether conditions are right to allow a jump:
  147. if (jump && isGrounded)
  148. {
  149. Jump();
  150. }
  151. }
  152. private float jumpTimer;
  153. public void Jump()
  154. {
  155. isGrounded = false;
  156. jumpTimer = 0.1f;
  157. animator.applyRootMotion = false;
  158. rigidbody.position += Vector3.up * 0.03f;
  159. Vector3 velocity = rigidbody.velocity;
  160. velocity.y = jumpVelocity;
  161. rigidbody.velocity = velocity;
  162. }
  163. }
  164. }