Advertisement
Guest User

Untitled

a guest
Jul 17th, 2019
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.48 KB | None | 0 0
  1. // ================================================================================ //
  2. // PLAYER MOVEMENT //
  3. // -------------------------------------------------------------------------------- //
  4. // This script handles 2D character movement and the associated //
  5. // sprite animation. //
  6. // This script makes use of the Animator, Transform and Rigidbody2D components. //
  7. // There is a flag to toggle between Ridgidbody2D and direct transform updates. //
  8. // -------------------------------------------------------------------------------- //
  9. // Author: Joseph Breslin (2019) //
  10. // ================================================================================ //
  11.  
  12. using UnityEngine;
  13.  
  14. public class PlayerMovement : MonoBehaviour {
  15.  
  16. enum Direction { DOWN=0, UP=1, LEFT=2, RIGHT=3 };
  17. Direction direction; // Use direction enum to clearly denote the animator's integer parameter.
  18. Animator animator;
  19. Rigidbody2D rb2D;
  20. Vector3 move; // 'move' is used to apply velocity when Rigidbody2D is utilised.
  21. // 'move' is used as an additive to the player's position when Rigidbody2D is not utilised.
  22. float playerSpeed; // 'playerSpeed' is utilised as a multiplier to 'move'. It is updated by sprint input.
  23. bool isRunning = false;
  24.  
  25. [Range(0, 20)] public float walkSpeed = 1.5f; // Designer sets walk speed in editor, default is 1.5.
  26. [Range(0, 20)] public float sprintSpeed = 3f; // Designer sets sprint speed in editor, default is 3.
  27.  
  28. public float walkAnimationSpeed = 1f; // Animation speed settings for walking.
  29. public float sprintAnimationSpeed = 1.2f; // Animation speed settings for sprinting.
  30.  
  31. public bool isRigidBody = false; // Flag to disable/enable rigidbody movement. Transform component updates are applied if 'isRigidBody' is set to false.
  32. public string sprintInput = "run"; // Input setting for sprint button (assigned to left shift).
  33. public string xAxis = "Horizontal"; // Input setting for horizontal movement.
  34. public string yAxis = "Vertical"; // Input setting for vertical movement.
  35.  
  36. private void Start()
  37. {
  38. animator = GetComponent();
  39. rb2D = GetComponent();
  40. playerSpeed = walkSpeed; //Initialise 'playerSpeed' with the 'walkSpeed' value.
  41. }
  42.  
  43. private void Update()
  44. {
  45. isRunning = Input.GetButton(sprintInput); //Continuesly poll for sprint input and assign the value to 'isRunning'.
  46. }
  47.  
  48. void FixedUpdate()
  49. { //Use fixed update to avoid frame rate variance of player movement.
  50. if (isRunning) //Check to see if sprint mechanic is enabled.
  51. {
  52. playerSpeed = sprintSpeed; //Assign 'sprintSpeed' to 'playerSpeed' and update the animator paramater 'Speed' with the sprint animation speed value.
  53. animator.SetFloat("Speed", sprintAnimationSpeed);
  54. }
  55. else
  56. {
  57. playerSpeed = walkSpeed; //Assign 'walkSpeed' to 'playerSpeed' and update the animator paramater 'Speed' with the walk animation speed value.
  58. animator.SetFloat("Speed", walkAnimationSpeed);
  59. }
  60.  
  61. //INPUT
  62. move = new Vector3(Input.GetAxisRaw(xAxis), Input.GetAxisRaw(yAxis)); // Assign the input axis values to the x and y paramaters of 'move'.
  63. move = (move.magnitude > 1.0f) ? move = move.normalized : move; // Check to see if the magnitude of the move vector is greater than one.
  64. // If so the player will move too fast in diagonal directions.
  65. // Reduce peaks in velocity by normalizing the vector when the magnitude is greater than 1.
  66. //ANIMATION
  67. bool isMoving;
  68. isMoving = move.magnitude < .00001 ? false : true; //Return false if player movement input has ceased.
  69. animator.SetBool("Is_Moving", isMoving); //Update the Animator parameter that enables movement animations.
  70.  
  71. if (Mathf.Abs(move.x) > Mathf.Abs(move.y)) //Determine and assign the appropriate direction by checking 'move' x and y co-ordinates.
  72. {
  73. if (move.x > 0)
  74. {
  75. direction = Direction.RIGHT;
  76. }
  77. else if (move.x < 0)
  78. {
  79. direction = Direction.LEFT;
  80. }
  81. }
  82. else
  83. {
  84. if (move.y > 0)
  85. {
  86. direction = Direction.UP;
  87. }
  88. else if (move.y < 0)
  89. {
  90. direction = Direction.DOWN;
  91. }
  92. }
  93.  
  94. int dir = (int)direction; // Enum integer conversion.
  95. animator.SetInteger("Direction", dir); // Set Animator parameter to update the direction in which the player animation will face.
  96.  
  97. //MOVEMENT
  98. if (!isRigidBody) // When Rigidbody2D is disabled: Multiply 'move' by the time delta and 'playerSpeed'.
  99. { // Then add this value to the player transform component position, so to move the player.
  100. transform.position += move * Time.deltaTime * playerSpeed;
  101. }
  102. else
  103. {
  104. if (!isMoving) // When Rigidbody2D is enabled: Multiply 'move' by the 'playerSpeed'. Then if the "isMoving" flag is true, assign this value to the rigidBody's velocity.
  105. {
  106. rb2D.velocity = Vector3.zero; // When the "isMoving" flag is false, Assign a vector3 of (0,0,0) to the rigidBody's velocity thus halting player movement.
  107. }
  108. else
  109. {
  110. rb2D.velocity = move * playerSpeed;
  111. }
  112. }
  113. }
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement