how long a bottom being pressed [solved]
Hi guys i made jump script and it works good but problem is.....i want to say if player Tap the jump bottom.make Small jump. and if played Hold the jump bottom. make Big jump.
i need something like this....
if ( player was grounded and input.getbottondown( jump) for 0.01f second)
myrigidbody.velocity=new Vector2 (0,jumppower);
if ( player was grounded and input.getbottondown( jump) for 0.03f second)
myrigidbody.velocity=new Vector2 (0,jumppower+5);
Answer by Hellium · Mar 03, 2018 at 02:25 PM
I would try something like this:
public float bigJumpThreshold = 0.03f;
private float jumpButtonHoldDuration ;
void Update()
{
if( Input.GetButton("jump") )
{
jumpButtonHoldDuration += Time.deltaTime ;
}
if( grounded )
{
if( jumpButtonHoldDuration >= bigJumpThreshold )
Jump( jumppower + 5 ) ;
else if ( Input.GetButtonUp("jump") )
Jump( jumppower ) ;
}
}
private void Jump( float power )
{
myrigidbody.velocity = new Vector2(myrigidbody.velocity.x, power);
jumpButtonHoldDuration = 0 ;
}
Answer by tw1st3d · Mar 03, 2018 at 03:45 AM
Try something like this.
if (Input.GetButton("Fire1") {
if (Input.GetButtonDown("Fire1"){
//small jump
} else {
//large jump
}
}
your code is working but it's bugy . some time making small jump, and sometimes making large jump..
how can i say.... if player pressed jump button fast ,make small jump . and if player pressed jump button not fast ,make large jump .
if (grounded && Input.GetButton ("Jump")) {
myanim.SetBool ("isgrounded", false);
if (Input.GetButtonDown ("Jump")) {
myrig.velocity = new Vector2 (myrig.velocity.x, jumppower);
} else {
myrig.velocity = new Vector2 (myrig.velocity.x, jumppower+5);
}
}
Answer by samooniyakk · Mar 03, 2018 at 03:03 PM
Thank you mr.hellium . i added one line code to your script and now your script working very very good .thanx so much
if (Input.GetButton("Jump"))
{
jumpbuttonholdduration += Time.deltaTime;
}
if (grounded)
{
if (jumpbuttonholdduration >= bigjumpthreshold)
Jump (jumppower + 5);
else if (Input.GetButtonUp("Jump"))
Jump (jumppower);
}
//if player was not grounded reset the jumpbuttonholdduration;
if (!grounded) {
jumpbuttonholdduration = 0;
}