player keeps momentum after landing jump

hello,
I made a script for my game that stops the player and charges jump when space button is pressed and when I release it the player jumps in a predefined way (just like Jumpking) but from some reason when the player object lands instead of stopping on spot it lands it keeps some momentum from the jump and slide on the x-axis and I can’t really figure out why is it happening. I tried to stop the player with Oncollisionenter but it did not really work and I’m not sure how would the code have to look like.

thanks for every idea

public class jumpkingJump : MonoBehaviour
{
    //groundcheck variables
    private bool Grounded;
    public LayerMask WhatIsGround;
    private Transform GroundCheck;
    private float GroundedRadius = .2f;

    //jump variables
    private float JumpPressure = 0f;
    public float minJump = 4f;
    public float maxjump = 20f;
    private float StopSpeed;

    //movement
    public float moveSpeed = 5f;
    public const string RIGHT = "right";
    public const string LEFT = "left";
    public const string MIDDLE = "middle";
    string ButtonPressed;


    private Rigidbody2D m_Rigidbody2D;


    // Update is called once per frame

    private void Awake()
    {
        GroundCheck = transform.Find("GroundCheck");
        m_Rigidbody2D = transform.GetComponent<Rigidbody2D>();
    }

    void FixedUpdate ()
    {
        //check if player is standing on the ground
        Grounded = false;
        Collider2D[] colliders = Physics2D.OverlapCircleAll(GroundCheck.position, GroundedRadius, WhatIsGround);
        for (int i = 0; i < colliders.Length; i++)
        {
            if (colliders[i].gameObject != gameObject)
                Grounded = true;

        }

    }
    void Update()
    {
        if (Grounded)
        {
            //movement right
            if (Input.GetKey(KeyCode.D))
            {
                ButtonPressed = RIGHT;
                transform.Translate(Vector2.right * (Time.deltaTime * moveSpeed));

            }

            //movement left
            else if (Input.GetKey(KeyCode.A))
            {
                ButtonPressed = LEFT;
                transform.Translate(Vector2.left * (Time.deltaTime * moveSpeed));

            }
            else
            {
                ButtonPressed = MIDDLE;
            }
        }

        //Jump if grounded
        if (Grounded)
        {
            if (Input.GetButton("Jump"))
            {
                StopSpeed = moveSpeed;
                moveSpeed = 0f;
                if(JumpPressure < maxjump)
                {
                    JumpPressure += Time.deltaTime * 15f;
                }
                else
                {
                    JumpPressure = maxjump;
                }
               
            }
            //release button
            else
            {
                //jump//
                if (JumpPressure > 0f)
                {
                    //deciding which trajectory depending on variable Button Pressed
                    if (ButtonPressed == RIGHT)
                    {
                        JumpPressure = JumpPressure + minJump;
                        m_Rigidbody2D.velocity = new Vector2(8f, JumpPressure);
                    }
                    else if (ButtonPressed == LEFT)
                    {
                        JumpPressure = JumpPressure + minJump;
                        m_Rigidbody2D.velocity = new Vector2(-8f, JumpPressure);
                    }
                    else
                    {
                        JumpPressure = JumpPressure + minJump;
                        m_Rigidbody2D.velocity = new Vector2 (0f, JumpPressure);
                    }
                    JumpPressure = 0f;
                    moveSpeed = 5;
                   
                }
            }

        }
    }
    void OnCollisionEnter2D(Collision2D col)
    {
        if(col.gameObject.tag == "ground")
        {
            m_Rigidbody2D.velocity = Vector2.zero;
        }
    }
}

Not sure this is your problem, or whether it is the appropriate solution.
Do you have a physics2d material on your feet collider, that could give you some grip.
Then if you are sticking to the walls you could add another collider that is slippery to help you slide down the walls, this could be on empty object child of your player. If this is bad method hopefully someone else will chime in.
I am using capsule collider2d horizontal for my feet collider, with a physics2d material.

1 Like