Search Unity

"2D JUMP Is Ground Check

Discussion in '2D' started by Timotyender8, Jan 19, 2019.

  1. Timotyender8

    Timotyender8

    Joined:
    Dec 30, 2017
    Posts:
    4
    I hello i am very new to unity and this is my first platformer. I made my player jump and implemented my ground check with just a trigger enter 2d and it all workes fine but after some jumps my player just stops jumping here is my code:
    Code (CSharp):
    1. public class Player : MonoBehaviour
    2. {
    3.     [SerializeField]
    4.     private int Player_Move_Speed;
    5.     [SerializeField]
    6.     private int Jump_height;
    7.     private bool CanJump = false;
    8.    
    9.     void Start()
    10.     {
    11.        
    12.     }
    13.  
    14.  
    15.     void Update()
    16.     {
    17.         Movement();
    18.  
    19.     }
    20.     private void Movement()
    21.     {
    22.         if (Input.GetKey(KeyCode.A))
    23.         {
    24.             transform.Translate(Vector3.left * Player_Move_Speed * Time.deltaTime);
    25.  
    26.         }
    27.         if (Input.GetKey(KeyCode.D))
    28.         {
    29.             transform.Translate(Vector3.right * Player_Move_Speed * Time.deltaTime);
    30.  
    31.         }
    32.         if (CanJump == true)
    33.         {
    34.             if (Input.GetKeyDown(KeyCode.W))
    35.             {
    36.                 GetComponent<Rigidbody2D>().AddForce(new Vector2(0, Jump_height), ForceMode2D.Impulse);
    37.                 CanJump = false;
    38.  
    39.             }
    40.         }
    41.         if (CanJump == false)
    42.         {
    43.             return;
    44.         }
    45.  
    46.        
    47.  
    48.  
    49.     }
    50.     private void OnTriggerEnter2D(Collider2D other)
    51.     {
    52.         if(other.tag== "GROUND")
    53.         {
    54.             Debug.Log("landed");
    55.             CanJump = true;
    56.         }
    57.  
    58.     }
    59. }
     
  2. Primoz56

    Primoz56

    Joined:
    May 9, 2018
    Posts:
    369
    Put a few more debug.logs in, i assume you character either gets 'stuck' in the ground and can't be accelerated out of it, or he's levitating exactly at 0 or something like that.
     
  3. FantasticGlass

    FantasticGlass

    Joined:
    May 31, 2016
    Posts:
    38
    There might be some instances where CanJump is false and you have already entered the ground collider so nothing is going to trigger to change CanJump back to true.

    Checking if you are on ground every frame in Update(), or every physics update in FixedUpdate() is one possible solution.

    You could try changing the method to OnTriggerStay2D.
    That might work for you.

    This is more costly cpu wise, but if your game is small then you shouldn't have to worry about it.
    Better to get your game working and fun, then worry about performance.

    also you can remove:
    Code (CSharp):
    1. if (CanJump == false)
    2. {
    3.      return;
    4. }
    as it is the last thing in the method anyways, it is not preventing any other code from being run, so it does not do anything.

    Your code looks well structured and well thought out.
    Keep up the good work!
     
  4. PRABHBIR123

    PRABHBIR123

    Joined:
    Apr 9, 2017
    Posts:
    72
    @Timotyender8 check out this video on more info about jumping