Advertisement
Guest User

Untitled

a guest
Mar 23rd, 2019
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.46 KB | None | 0 0
  1. using UnityEngine;
  2.  
  3. // MoveBehaviour inherits from GenericBehaviour. This class corresponds to basic walk and run behaviour, it is the default behaviour.
  4. public class MoveBehaviour : GenericBehaviour
  5. {
  6. public float walkSpeed = 0.15f; // Default walk speed.
  7. public float runSpeed = 1.0f; // Default run speed.
  8. public float sprintSpeed = 2.0f; // Default sprint speed.
  9. public float speedDampTime = 0.1f; // Default damp time to change the animations based on current speed.
  10. public string jumpButton = "Jump"; // Default jump button.
  11. public float jumpHeight = 1.5f; // Default jump height.
  12. public float jumpIntertialForce = 10f; // Default horizontal inertial force when jumping.
  13.  
  14. private float speed, speedSeeker; // Moving speed.
  15. private int jumpBool; // Animator variable related to jumping.
  16. private int groundedBool; // Animator variable related to whether or not the player is on ground.
  17. private bool jump; // Boolean to determine whether or not the player started a jump.
  18. private bool isColliding; // Boolean to determine if the player has collided with an obstacle.
  19.  
  20. // Start is always called after any Awake functions.
  21. void Start()
  22. {
  23. // Set up the references.
  24. jumpBool = Animator.StringToHash("Jump");
  25. groundedBool = Animator.StringToHash("Grounded");
  26. behaviourManager.GetAnim.SetBool (groundedBool, true);
  27.  
  28. // Subscribe and register this behaviour as the default behaviour.
  29. behaviourManager.SubscribeBehaviour (this);
  30. behaviourManager.RegisterDefaultBehaviour (this.behaviourCode);
  31. speedSeeker = runSpeed;
  32. }
  33.  
  34. // Update is used to set features regardless the active behaviour.
  35. void Update ()
  36. {
  37. // Get jump input.
  38. if (!jump && Input.GetButtonDown(jumpButton) && behaviourManager.IsCurrentBehaviour(this.behaviourCode) && !behaviourManager.IsOverriding())
  39. {
  40. jump = true;
  41. }
  42. }
  43.  
  44. // LocalFixedUpdate overrides the virtual function of the base class.
  45. public override void LocalFixedUpdate()
  46. {
  47. // Call the basic movement manager.
  48. MovementManagement(behaviourManager.GetH, behaviourManager.GetV);
  49.  
  50. // Call the jump manager.
  51. JumpManagement();
  52. }
  53.  
  54. // Execute the idle and walk/run jump movements.
  55. void JumpManagement()
  56. {
  57. // Start a new jump.
  58. if (jump && !behaviourManager.GetAnim.GetBool(jumpBool) && behaviourManager.IsGrounded())
  59. {
  60. // Set jump related parameters.
  61. behaviourManager.LockTempBehaviour(this.behaviourCode);
  62. behaviourManager.GetAnim.SetBool(jumpBool, true);
  63. // Is a locomotion jump?
  64. if(behaviourManager.GetAnim.GetFloat(speedFloat) > 0.1)
  65. {
  66. // Temporarily change player friction to pass through obstacles.
  67. GetComponent<CapsuleCollider>().material.dynamicFriction = 0f;
  68. GetComponent<CapsuleCollider>().material.staticFriction = 0f;
  69. // Set jump vertical impulse velocity.
  70. float velocity = 2f * Mathf.Abs(Physics.gravity.y) * jumpHeight;
  71. velocity = Mathf.Sqrt(velocity);
  72. behaviourManager.GetRigidBody.AddForce(Vector3.up * velocity, ForceMode.VelocityChange);
  73. }
  74. }
  75. // Is already jumping?
  76. else if (behaviourManager.GetAnim.GetBool(jumpBool))
  77. {
  78. // Keep forward movement while in the air.
  79. if (!behaviourManager.IsGrounded() && !isColliding && behaviourManager.GetTempLockStatus())
  80. {
  81. behaviourManager.GetRigidBody.AddForce(transform.forward * jumpIntertialForce * Physics.gravity.magnitude * sprintSpeed, ForceMode.Acceleration);
  82. }
  83. // Has landed?
  84. if ((behaviourManager.GetRigidBody.velocity.y < 0) && behaviourManager.IsGrounded())
  85. {
  86. behaviourManager.GetAnim.SetBool(groundedBool, true);
  87. // Change back player friction to default.
  88. GetComponent<CapsuleCollider>().material.dynamicFriction = 0.6f;
  89. GetComponent<CapsuleCollider>().material.staticFriction = 0.6f;
  90. // Set jump related parameters.
  91. jump = false;
  92. behaviourManager.GetAnim.SetBool(jumpBool, false);
  93. behaviourManager.UnlockTempBehaviour(this.behaviourCode);
  94. }
  95. }
  96. }
  97.  
  98. // Deal with the basic player movement
  99. void MovementManagement(float horizontal, float vertical)
  100. {
  101. // On ground, obey gravity.
  102. if (behaviourManager.IsGrounded())
  103. behaviourManager.GetRigidBody.useGravity = true;
  104.  
  105. // Call function that deals with player orientation.
  106. Rotating(horizontal, vertical);
  107.  
  108. // Set proper speed.
  109. Vector2 dir = new Vector2(horizontal, vertical);
  110. speed = Vector2.ClampMagnitude(dir, 1f).magnitude;
  111. // This is for PC only, gamepads control speed via analog stick.
  112. speedSeeker += Input.GetAxis("Mouse ScrollWheel");
  113. speedSeeker = Mathf.Clamp(speedSeeker, walkSpeed, runSpeed);
  114. speed *= speedSeeker;
  115. if (behaviourManager.IsSprinting())
  116. {
  117. speed = sprintSpeed;
  118. }
  119.  
  120. behaviourManager.GetAnim.SetFloat(speedFloat, speed, speedDampTime, Time.deltaTime);
  121. }
  122.  
  123. // Rotate the player to match correct orientation, according to camera and key pressed.
  124. Vector3 Rotating(float horizontal, float vertical)
  125. {
  126. // Get camera forward direction, without vertical component.
  127. Vector3 forward = behaviourManager.playerCamera.TransformDirection(Vector3.forward);
  128.  
  129. // Player is moving on ground, Y component of camera facing is not relevant.
  130. forward.y = 0.0f;
  131. forward = forward.normalized;
  132.  
  133. // Calculate target direction based on camera forward and direction key.
  134. Vector3 right = new Vector3(forward.z, 0, -forward.x);
  135. Vector3 targetDirection;
  136. targetDirection = forward * vertical + right * horizontal;
  137.  
  138. // Lerp current direction to calculated target direction.
  139. if((behaviourManager.IsMoving() && targetDirection != Vector3.zero))
  140. {
  141. Quaternion targetRotation = Quaternion.LookRotation (targetDirection);
  142.  
  143. Quaternion newRotation = Quaternion.Slerp(behaviourManager.GetRigidBody.rotation, targetRotation, behaviourManager.turnSmoothing);
  144. behaviourManager.GetRigidBody.MoveRotation (newRotation);
  145. behaviourManager.SetLastDirection(targetDirection);
  146. }
  147. // If idle, Ignore current camera facing and consider last moving direction.
  148. if(!(Mathf.Abs(horizontal) > 0.9 || Mathf.Abs(vertical) > 0.9))
  149. {
  150. behaviourManager.Repositioning();
  151. }
  152.  
  153. return targetDirection;
  154. }
  155.  
  156. // Collision detection.
  157. private void OnCollisionStay(Collision collision)
  158. {
  159. isColliding = true;
  160. }
  161. private void OnCollisionExit(Collision collision)
  162. {
  163. isColliding = false;
  164. }
  165. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement