Search code examples
c#unity-game-enginecollider

Unity Tag and Collider problem while player is jumping


This is my player's jump code:

void Update()
{
    float oldmoveHorizontal = moveHorizontal;
    moveHorizontal = Joystick.Horizontal;
    moveVertical = Joystick.Vertical;    

    if (isJumping == false && moveVertical >= .7f)
    {
        moveVertical = Speed * 70;
        isJumping = true;
    }

    ball_move = new Vector2(moveHorizontal, moveVertical);         
    ball_rigid.AddForce(ball_move);

    void OnCollisionEnter2D(Collision2D col)
    {
        if (col.gameObject.tag == "Ground")
         {
             isJumping = false;
         }        
    }
}

When ball touches under of the collider it can jump again. How can i block this situation ? this is image

if you cant download: https://ibb.co/yVgXmrM


Solution

  • void OnCollisionEnter2D(Collision2D col)
    {
        if (col.gameObject.tag == "Ground")
        {
            foreach (ContactPoint2D item in col.contacts)
            {
                Debug.Log("Normal:" + item.normal);
                if (item.normal.y > 0 && col.gameObject.tag == "Ground")
                {
                    isJumping = false;
                    Debug.Log("Top of Collider");
                }
            }
        }}
    

    I found my solution with checking top side of collider. With this code way, if player touchs top side of collider, it's going to activate jump again.