Jumping next to collider don't work

Hello I am trying to make 3D game but I have a problem with jumping near colliders.

If I jump next to collider it works but as soon as I start walking towards the block with collider and jump, it starts glitching and don’t want to jump

I’m not sure what is the issue, maybe something with my groundCheck object

Here’s the code:

using UnityEngine;

public class Movement : MonoBehaviour
{
    public CharacterController Player;
    public float speed = 12f;
    public float sprintSpeed = 24f;
    public float jumpHeight = 0.5f;

    public Transform groundCheck;
    public float groundDistance = 0.7f;
    public LayerMask groundMask;

    public float gravity = -9.81f;
    public Camera myCam;

    Vector3 velocity;
    bool isGrounded;

    void Update()
    {
        isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);
        if(isGrounded && velocity.y < 0)
        {
            velocity.y = -2f;
        }

        float z = Input.GetAxis("Vertical");
        float x = Input.GetAxis("Horizontal");

        Vector3 move = transform.right * x + transform.forward * z;

        if(Input.GetKey("left shift"))
        {
            Player.Move(move * sprintSpeed * Time.deltaTime);
            myCam.fieldOfView += 0.2f;
            if(myCam.fieldOfView >= 70)
            {
                myCam.fieldOfView = 70;
            }
        }
        else
        {
            Player.Move(move * speed * Time.deltaTime);
            myCam.fieldOfView -= 0.2f;
            if (myCam.fieldOfView <= 60)
            {
                myCam.fieldOfView = 60;
            }
        }

        if(isGrounded && Input.GetButton("Jump"))
        {
            velocity.y = Mathf.Sqrt(jumpHeight * -2 * gravity);
        }
        velocity.y += gravity * Time.deltaTime;

        Player.Move(velocity * Time.deltaTime);

    }
}

Thank you for any advice

Not sure what’s causing the issue, but there are a few lines that might fix it.

if(isGrounded && velocity.y < 0)
        {
            velocity.y = -2f;
        }

This part basically says “if we’re grounded and we’re falling, make us fall more”, which is a little bit unintuitive because a person cannot have a downwards velocity if they’re grounded. I suspect you may have included this so that when your player comes back down, they continue falling all the way to the ground. But the only reason you’d need to do that is if isGrounded returned an innacurate result because your CheckSphere was an innacurate size. So I’d delete this bit. Personally I’d also replace GetSphere with a downwards Raycast, since it’s less likely to suffer problems.

            velocity.y = Mathf.Sqrt(jumpHeight * -2 * gravity);

I don’t think all this math is necessary for you. You can just do something like velocity.y = 10.0f;

        velocity.y += gravity * Time.deltaTime;

Be careful with this line. Overtime it will cause your player’s velocity to build up to a massive number. Eventually they might even fly through the floor! That’d be kinda funny but that’s not what you want. Only add gravitational velocity to the player if they’re falling (i.e. if isGrounded == false).

velocity.y += gravity * Time.deltaTime;

Player.Move(velocity * Time.deltaTime);

You are doubling-up on Time.deltaTime here. You only need to multiply the velocity by Time.deltaTime once. Personally I would remove it from the Player.Move part (only the one on line 58 I think) because then it will still work with your jump part. But be careful: once you remove it, your player will suddenly be jumping and falling a lot faster than he previously was, so you’ll wanna adjust that!

Hopefully implementing some of those things will fix your issue.