Search Unity

Player can't jump when moving left/right and jump higher in random moments

Discussion in '2D' started by Gray770, Nov 15, 2019.

  1. Gray770

    Gray770

    Joined:
    Nov 13, 2019
    Posts:
    2
    Hi, today when i was testing levels in game i saw that my character can't sometimes jump when moving.
    Second problem is that my character sometimes jump higher than he should (it usually happens when you are on edge of platform), any fixes?

    Player code:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Player : MonoBehaviour
    6. {
    7.  
    8.     private Rigidbody2D myRigidbody2D;
    9.  
    10.     [SerializeField]
    11.     private float moveSpeed;
    12.  
    13.     [SerializeField]
    14.     private Transform[] groundPoints;
    15.  
    16.     [SerializeField]
    17.     private float groundRadius;
    18.  
    19.     [SerializeField]
    20.     private LayerMask whatIsGround;
    21.     private bool isGrounded;
    22.     private bool jump;
    23.  
    24.     [SerializeField]
    25.     private float jumpForce;
    26.  
    27.  
    28.  
    29.     void Start()
    30.     {
    31.         myRigidbody2D = GetComponent<Rigidbody2D>();
    32.     }
    33.  
    34.     // Update is called once per frame
    35.  
    36.     void FixedUpdate()
    37.     {
    38.         float horizontal = Input.GetAxis("Horizontal");
    39.         HandleMovement(horizontal);
    40.         isGrounded = IsGrounded();
    41.         ResetValues();
    42.     }
    43.  
    44.     private void HandleMovement(float horizontal)
    45.     {
    46.  
    47.         myRigidbody2D.velocity = new Vector2(horizontal * moveSpeed, myRigidbody2D.velocity.y);
    48.  
    49.         if (Input.GetKeyDown(KeyCode.UpArrow))
    50.         {
    51.             jump = true;
    52.         }
    53.         if (isGrounded && jump)
    54.         {
    55.             isGrounded = false;
    56.             myRigidbody2D.AddForce(new Vector2(0, jumpForce));
    57.             isGrounded = false;
    58.         }
    59.     }
    60.     private void ResetValues()
    61.     {
    62.         jump = false;
    63.     }
    64.  
    65.     private bool IsGrounded()
    66.     {
    67.         if (myRigidbody2D.velocity.y <= 0)
    68.         {
    69.             foreach (Transform point in groundPoints)
    70.             {
    71.                 Collider2D[] colliders = Physics2D.OverlapCircleAll(point.position, groundRadius, whatIsGround);
    72.  
    73.                 for (int i = 0; i < colliders.Length; i++)
    74.                 {
    75.                     if (colliders[i].gameObject != gameObject)
    76.                     {
    77.                         return true;
    78.                     }
    79.                 }
    80.             }
    81.         }
    82.         return false;
    83.     }
    84.  
    85.  
    86. }
    87.  
    88.  
    89.  
    90.  
    Code for better jumping:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class BetterJump : MonoBehaviour
    6. {
    7.     public float fallMultiplier = 2.5f;
    8.     public float lowMultiplier = 2.5f;
    9.  
    10.     Rigidbody2D rb;
    11.  
    12.     void Awake()
    13.     {
    14.         rb = GetComponent<Rigidbody2D>();
    15.     }
    16.  
    17.     private void Update()
    18.     {
    19.         if (rb.velocity.y < 0)
    20.         {
    21.             rb.velocity += Vector2.up * Physics2D.gravity.y * (fallMultiplier - 1) * Time.deltaTime;
    22.         }
    23.     }
    24. }
    25.  
     
  2. vakabaka

    vakabaka

    Joined:
    Jul 21, 2014
    Posts:
    1,153
    Input should be checked in the update
    Code (CSharp):
    1. float horizontal;
    2. void Update() {
    3. horizontal = Input.GetAxis("Horizontal");
    4. if (Input.GetKeyDown(KeyCode.UpArrow)) {
    5. jump = true;
    6. }
    7. }
    8.  
     
    MisterSkitz likes this.