G
G
Gleb_Shalygin2021-08-11 13:21:14
C++ / C#
Gleb_Shalygin, 2021-08-11 13:21:14

How to change the strength of the length and height of the jump with a long hold?

Good day, fellow programmers! Faced with one question. I hold down the space bar and after I release it, the character starts a long jump. I would like to make the hold time affect the length and height of the jump. That is, I hold the spacebar for 1 second and the character makes a low jump and flies not far, but if I hold the spacebar for 3-5 seconds, the character jumps higher and further accordingly. Help to implement this mechanic, I will be very grateful!

private void Update()
    {
if (isGrounded == true && Input.GetKeyUp(KeyCode.Space))
        {
            BallJump();
        }
}
 void BallJump() // прыжок вперед
    {
        Vector2 jjump = new Vector2(rb.velocity.x + speed, rb.velocity.y + jumpForce);
        rb.AddForce(jjump, ForceMode2D.Impulse);
        
    }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
NoNameDeveloper, 2021-08-11
@Gleb_Shalygin

using UnityEngine;

public class Jump : MonoBehaviour
{
    private float _power;

    // Methods

    public void Update()
    {
    	// При нажатие сбрасываем силу прыжка.
        if(Input.GetKeyDown(KeyCode.Space))
        	_power = 1;
        
        // При зажатие увеличиваем силу на некое число.
        else if(Input.GetKey(KeyCode.Space))
       		_power += .1f;

        // При отпускании сделаем прыжок с полученной силой.
        // Направление только нужно будет дописать.
        else if(Input.GetKeyUp(KeyCode.Space))
        	rb.AddForce(_power, ForceMode2D.Impulse);
    }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question