Advertisement
VorLex

jump issue

Nov 18th, 2022
938
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.46 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using System.Threading;
  4. using UnityEngine;
  5.  
  6. public class PlayerControls : MonoBehaviour
  7. {
  8.     // Components
  9.  
  10.     [Header("Movement Variables")]
  11.     [SerializeField] private float movementSpeed = 36.8f;
  12.     [SerializeField] private float maxMovementSpeed = 14f;
  13.     [SerializeField] private float linearDrag = 8.9f;
  14.     private float baseMovementSpeed;
  15.     private float baseMaxMovementSpeed;
  16.  
  17.     public float sprintMultiplier = 1.5f;
  18.     private bool isSprinting => Input.GetKeyDown(KeyCode.LeftShift);
  19.  
  20.     [Header("Jump Variables")]
  21.     [SerializeField] private float jumpForce = 11f;
  22.     [SerializeField] private float aerialLinearDrag = 2.5f;
  23.     [SerializeField] private float fallMultiplier = 8f;
  24.     [SerializeField] private float lowFallMultiplier = 5f;
  25.     private bool canJump => Input.GetButtonDown("Jump") && isGrounded;
  26.  
  27.  
  28.    
  29.     // Gravity Components
  30.     [Header("Ground Collision Variables")]
  31.     [SerializeField] private LayerMask groundLayer;
  32.     [SerializeField] private float groundRaycastLength;
  33.     private bool isGrounded;
  34.  
  35.  
  36.  
  37.     // Player RigidBodyControls
  38.  
  39.     private Rigidbody2D plrBody;
  40.     private float horizontalDirection;
  41.     private bool directionChanged => (plrBody.velocity.x > 0f && horizontalDirection < 0f) || (plrBody.velocity.x < 0f && horizontalDirection > 0f);
  42.  
  43.  
  44.  
  45.     // Raycast Functions
  46.  
  47.     private void CheckCollisions()
  48.     {
  49.         isGrounded = Physics2D.Raycast(transform.position, Vector2.down * groundRaycastLength, groundRaycastLength, groundLayer);
  50.     }
  51.  
  52.  
  53.  
  54.     // Player General Functions
  55.  
  56.     private Vector2 GetInput() => new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"));
  57.  
  58.     private void MoveCharacter()
  59.     {
  60.         plrBody.AddForce(new Vector2(horizontalDirection, plrBody.velocity.y) * (movementSpeed * sprintMultiplier));
  61.  
  62.         if (Mathf.Abs(plrBody.velocity.x) > (maxMovementSpeed * sprintMultiplier))
  63.         {
  64.             plrBody.velocity = new Vector2(Mathf.Sign(plrBody.velocity.x) * (maxMovementSpeed * sprintMultiplier), plrBody.velocity.y);
  65.         }
  66.     }
  67.  
  68.     private void ApplyGroundDrag()
  69.     {
  70.         if (Mathf.Abs(horizontalDirection) < 0.4 || directionChanged)
  71.         {
  72.             plrBody.drag = linearDrag;
  73.         } else
  74.         {
  75.             plrBody.drag = 0f;
  76.         }
  77.     }
  78.  
  79.  
  80.     // Player Mechanical Functions
  81.  
  82.     private void ApplyAerialDrag()
  83.     {
  84.         plrBody.drag = aerialLinearDrag;
  85.     }
  86.    
  87.     // This might be another issue, the velocity after the execution would be (0, 5000) and not changing it's value.
  88.     private void Jump()
  89.     {
  90.         plrBody.velocity = new Vector2(plrBody.velocity.x, 0f);
  91.         plrBody.AddForce(Vector2.up * jumpForce, ForceMode2D.Impulse);
  92.         print("Jumped!");
  93.     }
  94.    
  95.     // This is (what i believe is) part of the issue.
  96.     private void FallMultiplier()
  97.     {
  98.         if (!isGrounded)
  99.         {
  100.             if (!Input.GetKey(KeyCode.Space) && !isGrounded)
  101.             {
  102.                 print("FallMultiplier");
  103.                 plrBody.gravityScale = fallMultiplier;
  104.             }
  105.             else if (Input.GetKey(KeyCode.Space))
  106.             {
  107.                 plrBody.gravityScale = lowFallMultiplier;
  108.                 print("LowFallMultiplier");
  109.             }
  110.         }
  111.         else
  112.         {
  113.             plrBody.gravityScale = 1f;
  114.         }
  115.     }
  116.  
  117.     // Initialized Functions
  118.  
  119.     private void Awake()
  120.     {
  121.         plrBody = GetComponent<Rigidbody2D>();
  122.         baseMaxMovementSpeed = maxMovementSpeed;
  123.         baseMovementSpeed = movementSpeed;
  124.     }
  125.  
  126.     private void FixedUpdate()
  127.     {
  128.         CheckCollisions();
  129.         MoveCharacter();
  130.         if (canJump) Jump();
  131.  
  132.         if (isSprinting)
  133.         {
  134.             maxMovementSpeed *= sprintMultiplier;
  135.             movementSpeed *= sprintMultiplier;
  136.         } else
  137.         {
  138.             maxMovementSpeed = baseMaxMovementSpeed;
  139.             movementSpeed = baseMovementSpeed;
  140.         }
  141.  
  142.         if (isGrounded)
  143.         {
  144.             ApplyGroundDrag();
  145.         } else
  146.         {
  147.             ApplyAerialDrag();
  148.             FallMultiplier();
  149.         }
  150.     }
  151.  
  152.     private void Update()
  153.     {
  154.         horizontalDirection = GetInput().x;
  155.     }
  156.  
  157.     private void OnDrawGizmos()
  158.     {
  159.         Gizmos.color = Color.red;
  160.         Gizmos.DrawLine(transform.position, transform.position + Vector3.down * groundRaycastLength);
  161.     }
  162. }
  163.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement