Jumps wont vary in height based on input length

Im still trying to make my collectathon platformer and am STILL trying to make the Jump Height vary based off of the length the Jump Button is held down but it still jumps at the exact same heigths

The code is below, any ideas asto what Im doing wrong?

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

public class Player : MonoBehaviour
{
    [Header("Movement and Game Feel")]
    [Range(3,10)]
    public int speed = 5;
    [Range(3, 10)]
    public int jumpForce = 4;
    public LayerMask ground;
    [Min(0.05f)]
    public float checkForGround = 1.5f;

    private Rigidbody character;
    private bool isGrounded;
    private bool isJumping = false;
    private bool jumpIsPressed;

    private float moveX;
    private float moveZ;

    // Start is called before the first frame update
    void Start()
    {
        character = GetComponent<Rigidbody>();
    }

    // Update is called once per frame
    void Update()
    {
        RaycastHit groundDetected;
        isGrounded = Physics.Raycast(transform.position, Vector3.down, out groundDetected, checkForGround, ground);
        inputformove();
        movements();
    }

    void inputformove()
    {
        moveX = Input.GetAxis("Horizontal");
        moveZ = Input.GetAxis("Vertical");
        jumpIsPressed = Input.GetButtonDown("Jump");
    }

    void movements()
    {
        character.velocity = new Vector3(moveX * speed, character.velocity.y, moveZ * speed);
        if(isGrounded && jumpIsPressed)
        {
            isJumping = !isJumping;
            character.AddForce(Vector3.up * jumpForce, ForceMode.Impulse);
            if(isJumping = true && Input.GetButtonUp("Jump"))
            {
                jumpPhysics();
            }
        }
    }

    private void jumpPhysics()
    {
        if(isJumping)
        {
            if(character.velocity.y > 0.25f)
            {
                jumpForce = jumpForce / 2;
            }
            else if(character.velocity.y > 0.0f)
            {
                jumpForce = jumpForce / 3;
            }
        }
    }
}

This will only return true on one single frame, no matter how long you hold the key down.

What you propose is tricky to get consistent. Update() frames are irregular in time spacing (by design). You could ask if the Jump button is down in FixedUpdate() (not if it WENT down, just if it IS down… be sure you understand that difference) and add more upwards force the longer you hold it, up until the limit of how high you want to jump.

But remember even with FixedUpdate() it is still probably hitting 50 times per second and not guaranteed spaced out in ANY way, and human fingers aren’t super nimble that way.

“Not if it WENT down just if it IS down” does that mean use input.getbutton(“Jump”) instead?

Yes.

1 Like

Exactly.

I’ve wanted to have this mechanism in my demo project for a while so I just made it and threw it into my ProximityButtons project, right next to the Coyote Jump demo (separate).

It demos both counting FixedUpdate() frames to look up height in a table, and also counting Time.deltaTime in Update() to look up jump height in an AnimationCurve.

The script:

https://github.com/kurtdekker/proximity_buttons/blob/master/proximity_buttons/Assets/DemoHoldForHigherJump/DemoHoldForHigherJump.cs

Full scene with instructions and debug widgets all over the place in the project.

proximity_buttons is presently hosted at these locations:

https://bitbucket.org/kurtdekker/proximity_buttons

https://github.com/kurtdekker/proximity_buttons

https://gitlab.com/kurtdekker/proximity_buttons

https://sourceforge.net/projects/proximity-buttons/