Advertisement
Guest User

Untitled

a guest
Mar 24th, 2019
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.27 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class PlayerCharacterController : MonoBehaviour {
  6. //internal components
  7. Rigidbody2D rigidBody;
  8. BoxCollider2D boxCollider;
  9.  
  10. //constants
  11. const float deadZone = 0.25f; //axis input deadzone
  12. const float groundedProjection = 0.08f; //how far into the ground to project the linecasts
  13.  
  14. //gameplay (DOCS: These settings are calibrated for 1 pixel = 1 unity unit)
  15. [Header("Movement Settings")]
  16. public float gravityScale = 100;
  17. public float moveForce = 1000f;
  18. public float jumpForce = 30000;
  19. public float maxSpeed = 300f;
  20. public float fallSpeed = 800f;
  21.  
  22. //basic movement
  23. float horizontalInput = 0f;
  24. float verticalInput = 0f;
  25. bool jumping = false;
  26. bool grounded = false;
  27.  
  28. void Awake() {
  29. rigidBody = GetComponent<Rigidbody2D>();
  30. boxCollider = GetComponent<BoxCollider2D>();
  31. }
  32.  
  33. void Start() {
  34. //
  35. }
  36.  
  37. void Update() {
  38. HandleInput();
  39. }
  40.  
  41. void FixedUpdate() {
  42. //debug
  43. rigidBody.gravityScale = gravityScale;
  44.  
  45. HandleMovement();
  46. }
  47.  
  48. void HandleInput() {
  49. //determine if on the ground (using coyote time)
  50. bool trueGrounded;
  51. trueGrounded = Physics2D.Linecast(transform.position + new Vector3(boxCollider.offset.x, boxCollider.offset.y, 0), transform.position + new Vector3(transform.localScale.x * (boxCollider.offset.x), -groundedProjection, 0), 1 << LayerMask.NameToLayer("Ground"));
  52. trueGrounded |= Physics2D.Linecast(transform.position + new Vector3(boxCollider.offset.x, boxCollider.offset.y, 0), transform.position + new Vector3(transform.localScale.x * (boxCollider.offset.x + boxCollider.size.x / 2.1f), -groundedProjection, 0), 1 << LayerMask.NameToLayer("Ground"));
  53. trueGrounded |= Physics2D.Linecast(transform.position + new Vector3(boxCollider.offset.x, boxCollider.offset.y, 0), transform.position + new Vector3(transform.localScale.x * (boxCollider.offset.x - boxCollider.size.x / 2.1f), -groundedProjection, 0), 1 << LayerMask.NameToLayer("Ground"));
  54.  
  55. if (trueGrounded) {
  56. grounded = true;
  57. } else {
  58. StartCoroutine(SetGroundedAfter(false, 0.1f)); //coyote physics: 100ms
  59. }
  60.  
  61. //get inputs
  62. verticalInput = GamePad.GetAxis(CAxis.LY);
  63. horizontalInput = GamePad.GetAxis(CAxis.LX);
  64.  
  65. //flip direction
  66. if (Mathf.Abs(horizontalInput) >= deadZone) {
  67. transform.localScale = new Vector3(horizontalInput > 0 ? 1 : -1, 1, 1);
  68. }
  69.  
  70. //determine if jumping
  71. if (GamePad.GetState().Pressed(CButton.A) && grounded) {
  72. jumping = true;
  73. }
  74. }
  75.  
  76. void HandleMovement() {
  77. //stop the player if input in that direction has been removed
  78. if (horizontalInput * rigidBody.velocity.x <= 0 && grounded) {
  79. rigidBody.velocity = new Vector2 (rigidBody.velocity.x * 0.85f, rigidBody.velocity.y);
  80. }
  81.  
  82. //move in the inputted direction, if not at max speed
  83. if (horizontalInput * rigidBody.velocity.x < maxSpeed) {
  84. rigidBody.AddForce (Vector2.right * horizontalInput * moveForce);
  85. }
  86.  
  87. //slow the player down when it's travelling too fast
  88. if (Mathf.Abs(rigidBody.velocity.x) > maxSpeed) {
  89. rigidBody.velocity = new Vector2 (Mathf.Sign(rigidBody.velocity.x) * maxSpeed, rigidBody.velocity.y);
  90. }
  91.  
  92. if (rigidBody.velocity.y < -fallSpeed) {
  93. rigidBody.velocity = new Vector2 (rigidBody.velocity.x, Mathf.Sign(rigidBody.velocity.y) * fallSpeed);
  94. }
  95.  
  96. //jump up
  97. if (jumping) {
  98. rigidBody.velocity = new Vector2(rigidBody.velocity.x, 0f); //max v-jump speed
  99. rigidBody.AddForce (new Vector2 (0f, jumpForce));
  100. jumping = false;
  101. }
  102. }
  103.  
  104. void OnDrawGizmos() {
  105. if (boxCollider != null) {
  106. Gizmos.color = Color.red;
  107. Gizmos.DrawLine(transform.position + new Vector3(boxCollider.offset.x, boxCollider.offset.y, 0), transform.position + new Vector3(transform.localScale.x * (boxCollider.offset.x), -groundedProjection, 0));
  108. Gizmos.DrawLine(transform.position + new Vector3(boxCollider.offset.x, boxCollider.offset.y, 0), transform.position + new Vector3(transform.localScale.x * (boxCollider.offset.x + boxCollider.size.x / 2.1f), -groundedProjection, 0));
  109. Gizmos.DrawLine(transform.position + new Vector3(boxCollider.offset.x, boxCollider.offset.y, 0), transform.position + new Vector3(transform.localScale.x * (boxCollider.offset.x - boxCollider.size.x / 2.1f), -groundedProjection, 0));
  110. }
  111. }
  112.  
  113. IEnumerator SetGroundedAfter(bool value, float delay) {
  114. yield return new WaitForSeconds(delay);
  115. grounded = value;
  116. }
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement