Problem with physics on a game object

Hello everyone!

I have a problem with one of my movement scripts, it compiles and works but I encountered an issue while testing it. If a player is standing next to a wall in my game and they continue to jump the player character will not jump but instead continue to keep adding the force to the object. As soon as the player moves away from the wall they are launched into the air far greater than they should be.

Here is my script!
public class playerJump : MonoBehaviour {

    // Use this for initialization
    public float jumpForce = 2.0f;
    private Rigidbody rig;
    public bool onGround = true;
    int jumpCount = 1;
    Vector3 jump = new Vector3(0.0f, 2.0f, 0.0f);
    void Start () {
        rig = GetComponent<Rigidbody>();
	}

    private void OnCollisionStay(Collision collision)
    {
        onGround = true;
        jumpCount = 1;
    }

    // Update is called once per frame
    void LateUpdate () {
        

        if (Input.GetKeyDown(KeyCode.Space) && onGround || jumpCount > 0 && Input.GetKeyDown(KeyCode.Space))
        {
           
            rig.AddForce(jump * jumpForce, ForceMode.Impulse);
            onGround = false;
            Debug.Log(jumpCount);
            jumpCount--;
        }

		
	}
}

I was just having this problem myself. Ships getting stuck to walls and such. Turns out, “Physic Material” is your friend. Create one, and apply it to the collision mesh on your character and tweak the values to your liking. Personally I set all my parameters to 0.1 and “Minimum”.

This should allow your player to slide up and down walls, though since your not checking what you’re colliding with, you might still have an issue where because you’re colliding with the wall, it will set onGround to true.

A solution to that may be to throw an if statement in your OnCollisionStay. Something like the following may do the trick.

    private void OnCollisionStay(Collision collision)
    {

        if (collision.collider.tag == "Ground")
        {
        onGround = true;
        jumpCount = 1;
        }
                
    }