Inconsistent Jump Height in a platformer game

The title explain itself, the jump is pretty much random, sometimes high, sometimes med, sometimes lower and sometimes it goes as it should be which is really annoying for a platformer mario like game, so here is the script below

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
using Cinemachine;
public class PlayerMovementTutorial : MonoBehaviour
{
    [Header("Movement")]
    public float moveSpeed;

    public float groundDrag;
    public float AirDrag;
    public float jumpspeed;
    public float gravityscale;
    public float fallinggravityscale;
    public float currentgravityscale;
    public float jumpCooldown;
    public float airMultiplier;
    public bool readyToJump;
    public AudioSource audioSource;
    public AudioClip clip;
    [HideInInspector] public float walkSpeed;
    [HideInInspector] public float sprintSpeed;

    [Header("Keybinds")]
    public KeyCode jumpKey = KeyCode.Space;

    [Header("Ground Check")]
    public float playerHeight;
    public LayerMask whatIsGround;
    public bool grounded;
    public ParticleSystem land;
    public bool isLanded;
    public Transform orientation;
    public Animator an;

    float horizontalInput;

    public CinemachineVirtualCamera cam;
    Vector3 moveDirection;

    public Rigidbody rb;
    public bool RotateL;
    public bool RotateR;
    public bool isMoving = true;
    public bool CanJump = true;

    private void Start()
    {
        an = GetComponent<Animator>();

        rb = GetComponent<Rigidbody>();
        rb.freezeRotation = true;

        readyToJump = true;
        currentgravityscale=gravityscale;
    }

    private void Update()
    {
        // ground check
        grounded = Physics.Raycast(transform.position, Vector3.down, playerHeight * 0.5f + 0.3f, whatIsGround);

        SpeedControl();

if (Input.GetKeyDown(KeyCode.Space) && readyToJump && grounded && CanJump)
        {

            Jump(); Invoke("Land", 0.9f);
            readyToJump = false;



            Invoke(nameof(ResetJump), jumpCooldown);
        }
        if(rb.velocity.y >= 0)
        {
             currentgravityscale=gravityscale;
        }
        else if(rb.velocity.y <0)
        {
            currentgravityscale=fallinggravityscale;
        }
        // handle drag
        if (grounded)
        {
            rb.drag = groundDrag;
            if (isLanded && !land.isPlaying)
            {
                isLanded = false;
                land.Play();
            }

        }
        else
        {
            rb.drag = AirDrag;

        }
    }

    private void FixedUpdate()
    {
        MyInput();
         rb.AddForce(Physics.gravity * (gravityscale -1) * rb.mass);
        if (isMoving)
        {
            MovePlayer();
            HandleRotation();
        }

    }

    private void MyInput()
    {
        horizontalInput = Input.GetAxisRaw("Horizontal");



        // when to jump
       
    }
    private void Land()
    {
        isLanded = true;
    }

    private void MovePlayer()
    {
        // calculate movement direction


        // on ground
        if (grounded)
        {
            rb.AddForce(moveDirection.normalized * moveSpeed * 10f, ForceMode.Force);
            an.SetBool("Fall", false);
        }

        // in air
        else if (!grounded)
        {
            rb.AddForce(moveDirection.normalized * moveSpeed * 10f * airMultiplier, ForceMode.Force);
            an.SetBool("Fall", true);
        }
    }

    private void SpeedControl()
    {
        Vector3 flatVel = new Vector3(rb.velocity.x, 0f, rb.velocity.z);

        // limit velocity if needed
        if (flatVel.magnitude > moveSpeed)
        {
            Vector3 limitedVel = flatVel.normalized * moveSpeed;
            rb.velocity = new Vector3(limitedVel.x, rb.velocity.y, limitedVel.z);
        }
    }

    public void Jump()
    {
        rb.velocity = new Vector3(rb.velocity.x, 0, rb.velocity.z);

        // reset y velocity
        rb.AddForce(Vector2.up * jumpspeed, ForceMode.Impulse);
        an.SetBool("Fall", true);

        an.SetTrigger("Jump");
        audioSource.clip = clip;
        audioSource.Play();
        an.ResetTrigger("Jump");
    }
    private void ResetJump()
    {
        readyToJump = true;

    }
    private void HandleRotation()
    {
        CinemachineFramingTransposer framingTransposer = cam.GetCinemachineComponent<CinemachineFramingTransposer>();
        if (Input.GetKey(KeyCode.A))
        {
            an.SetBool("Run", true);
            RotateL = true;
            RotateR = false;
        }
        else if (Input.GetKey(KeyCode.D))
        {
            an.SetBool("Run", true);
            RotateR = true;
            RotateL = false;
        }
        else
        {
            an.SetBool("Run", false);
        }

        if (RotateL == true)
        {
            moveDirection = horizontalInput * -orientation.transform.forward;

            transform.rotation = Quaternion.Euler(new Vector3(0, -180, 0));
            framingTransposer.m_TrackedObjectOffset = new Vector3(-132.38f, 25.7f, 0);
        }
        else if (RotateR == true)
        {
            moveDirection = horizontalInput * orientation.transform.forward;
            transform.rotation = Quaternion.Euler(new Vector3(0, 0, 0));
            framingTransposer.m_TrackedObjectOffset = new Vector3(132.38f, 25.7f, 0);
        }
    }

}

I have theories that the main problem is how grounded is set up
so I will try to change it to something else, but you can all give me your thoughts

It’s possible that for a few frames, you’re still “grounded” even though you’re jumping up. Because of this, you’d be applying drag to the character intended for the ground, which would significantly slow the jump. It’s different every time because Update cycles and FixedUpdate physics cycles aren’t in sync. So sometimes you’d have more or less physics updates with ground-drag while in the air. I’d recommend not using drag to handle slowing to a stop for movement.

But if you want to still use drag, maybe you could override the grounded check with a value of false if the rb velocity is moving up. That will at least ensure that you’re never getting a true grounded state if you’re jumping

Thanks @PixelDough
the problem was fixed by modifying the rb.velocity condition

 if(rb.velocity.y >= 0)
        {
            grounded=false;
             currentgravityscale=gravityscale;
        }

yeah, turns out I was that close of the solution
for the drag part. this is likely the best way to handle because if I let it be 0
the movement will be slippery and if I let it higher than 5 the jump will kinda won’t that fast
I don’t know actually, maybe I should give it a try to make a different way to balance since I actually changed the way of jumping
instead of it was depending on the jumpforce, it is now depend on the speed, gravity scale and fallinggravityscale so I guess if I let the drag high, the jump might not get affected but I will see about that

Thank you