Fixed the jump height consistency but result losing height

30 views Asked by At

I am working on my character jump base on Rigidbody.velocity.

here is my jump:

    [SerializeField] private float jumpPickHeight;
    [SerializeField] private float jumpPickTime;
    private float _jumpInitVelocith;
    private float _gravity;
    void OnEnable()
    {
        //jumped height
        _start_Y = _md.transform.position.y;
        CalculateInitVelocithAndGravity();

        _currentSpeed_Y = _jumpInitVelocith;
    }
    void Update()
    {
        //jumped height
        _jumpedHeight = _md.transform.position.y - _start_Y;

        _currentSpeed_Y = Mathf.Max(_currentSpeed_Y - _gravity * Time.deltaTime, 0);
        _rb.velocity += _currentSpeed_Y * Vector3.up;
    }
    void OnDisable()
    {
        _currentSpeed_Y = 0;
        _start_Y = 0;

        //jumped height
        Debug.Log(_jumpedHeight);
        _jumpedHeight = 0;
    }

and here is my calculation of jump data

    public void CalculateInitVelocithAndGravity()
    {
        if (jumpPickHeight <= 0 || jumpPickTime <= 0)
            return;

        _jumpInitVelocith = (2 * JumpPickHeight) / jumpPickTime;
        _gravity = (2 * JumpPickHeight) / (jumpPickTime * jumpPickTime);
    }

At first, i use Update() but causing jump height being not consist, _jumpedHeight result at around 1.8 - 2.1 when i set _jumpPickHeight to 2.

i fixed the consistency by just changing Update() to FixedUpdate(), but always result 1.8xxxxx. Since i saw the relation of

jumpPickHeight and jumpedHeight is linear,

1 result (lets say)0.92

2 result (lets say)1.84

so on, i cheese it by multiplying a error factor to minimize the amount of error (like let say 1.000023 for 1 and 2.000046 for 2)

    public const float JUMP_PICK_HEIGHT_ERROR = 1.0927f;
    public float JumpPickHeight { get => _jumpPickHeight * JUMP_PICK_HEIGHT_ERROR; }
    [SerializeField] float _jumpPickHeight;

but still not exactly equal the _jumpPickHeight i want.

tho I got it kind of working, i want to know what piece am I missing. how can I get rid of the error factor?

0

There are 0 answers