Search Unity

Other Character can jump when already in the air and i cant figure out whats allowing it in the script.

Discussion in 'Scripting' started by jh22899, Dec 3, 2022.

  1. jh22899

    jh22899

    Joined:
    Nov 2, 2022
    Posts:
    3
    As the title says, im working on a unity2d platformer for my first year uni assignment, and for some reason my character can jump constantly when in the air, i cant figure out why though. Not sure if its a scripting issue or something related to the characters child "groundcheck". any help would be sincearly appreciated. Also if this isnt the right thread could someone direct me to the correct place please aha.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerMovement : MonoBehaviour
    6. {
    7.     private float horizontal;
    8.     private float speed = 5f;
    9.     private float jumpingPower = 15f;
    10.     private bool isFacingRight = true;
    11.  
    12.     [SerializeField] private Rigidbody2D rb;
    13.     [SerializeField] private Transform groundCheck;
    14.     [SerializeField] private LayerMask groundLayer;
    15.  
    16.     void Update()
    17.         //determining if player is grounded for the jumping mechanic
    18.     {
    19.         horizontal = Input.GetAxisRaw("Horizontal");
    20.  
    21.         if (Input.GetButtonDown("Jump") && IsGrounded())
    22.         {
    23.             rb.velocity = new Vector2(rb.velocity.x, jumpingPower);
    24.         }
    25.  
    26.         //determining jump velocity
    27.         if (Input.GetButtonUp("Jump") && rb.velocity.y > 0f)
    28.         {
    29.             rb.velocity = new Vector2(rb.velocity.x, rb.velocity.y * 0.5f);
    30.         }
    31.  
    32.         //Controlling the jumping mechanic
    33.         //Adding force vertically
    34.         if (Input.GetKeyDown(KeyCode.Space))
    35.         {
    36.             rb.AddForce(Vector3.up * jumpingPower, ForceMode2D.Impulse);
    37.  
    38.         }
    39.  
    40.         Flip();
    41.     }
    42.  
    43.     private void FixedUpdate()
    44.     {
    45.         rb.velocity = new Vector2(horizontal * speed, rb.velocity.y);
    46.     }
    47.  
    48.  
    49.     private bool IsGrounded()
    50.     {
    51.         return Physics2D.OverlapCircle(groundCheck.position, 0.2f, groundLayer);
    52.     }
    53.  
    54.     //Flip mechanic
    55.     private void Flip()
    56.     {
    57.         if (isFacingRight && horizontal < 0f || !isFacingRight && horizontal > 0f)
    58.         {
    59.             isFacingRight = !isFacingRight;
    60.             Vector3 localScale = transform.localScale;
    61.             localScale.x *= -1f;
    62.             transform.localScale = localScale;
    63.         }
    64.     }
    65.  
     

    Attached Files:

    Last edited: Dec 3, 2022
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,486
    Please edit your post to use code-tags when posting code. Nobody wants to download files to look at code.

    Thanks.
     
  3. jh22899

    jh22899

    Joined:
    Nov 2, 2022
    Posts:
    3
    My bad, fixed the post :)
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,735
    You have three (3!!) different checks of the Jump button.

    The first also checks grounded.

    The second also asks questions about velocity

    The third asks nothing and just adds a force.

    You should have ONE (1) check of the Jump button.

    Try and unify and REMOVE all repeating code, a nice clean structure such as :

    Code (csharp):
    1. if (jump)
    2. {
    3.   // check other stuff
    4.   if (grounded)
    5.   {
    6.     // maybe check even more stuff!
    7.     if (velocity?)
    8.     {
    9.       // act to cause a jump
    10.     }
    11.   }
    12. }
    If you absolutely MUST act on the jump button elsewhere, capture it ONCE and set a variable, and use (and clear) that variable elsewhere.