How do I make my Rigidbody player not get stuck on walls?

My rigidbody player that I made keeps on getting stuck on walls if I jump and hold the direction key on the wall and I dont know how to fix it.164315-annotation-2020-07-29-183702.png

Here is the code I am using, mostly a mash up of code I used from tutorials because I am not an experienced programmer.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerMovement : MonoBehaviour
{
    /////////////////////
    ////variable zone////
    /////////////////////
    public Rigidbody playerBody;
    private Vector3 inputVector;
    public float speed = 10f;
    public Transform groundCheck;
    public float groundDistance = 0.4f;
    public LayerMask groundMask;
    private bool isGrounded;
    public float jumpHeight = 3f;
    /////////////////////
    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);

        inputVector = new Vector3(Input.GetAxis("Horizontal") * speed * 20 * Time.deltaTime, playerBody.velocity.y, Input.GetAxis("Vertical") * speed * 20 * Time.deltaTime);
        transform.LookAt(transform.position + new Vector3(inputVector.x, 0, inputVector.z));
        playerBody.velocity = inputVector;

        if(Input.GetButtonDown("BlobbyJump") | Input.GetButtonDown("AllJump") && isGrounded)
        {
            playerBody.AddForce(Vector3.up * jumpHeight, ForceMode.Impulse);
        }
    }
}

Unity has a Physics engine that tries to imitate real-world physics. In the real world, there is something called friction. When two objects are tightly touching each other they won’t move. Like a Hook-and-loop fastener.

To fix that in your game you can apply a Physics Material with 0 Friction to your player’s rigid body. This will tell Unity’s physics engine that your player won’t have any friction.

In my game, this prevents the player from being attached to a wall.


Screenshots of my player’s rigid body with a physics material:

164366-captura-de-pantalla-2020-07-30-a-las-04050.png


Documentation for 3D: Unity - Manual: Physic Material component reference

Documentation for 2D:

Hello,

you should use physics in FixedUpdate rather than Update. But test if player jumps in Update. With a boolean :

private bool is Jumped;

private void Update () {
    isJumped = (Input.GetButtonDown("BlobbyJump") | Input.GetButtonDown("AllJump")) && isGrounded;
}

private void FixedUpdate() {
    isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);
    
    if (isJumped)
       {
            isJumped = false;
            playerBody.AddForce(Vector3.up * jumpHeight, ForceMode.Impulse);
        }
}

Why not beginning with a more simple script ?

Sorry for any mistakes in English.