- Home /
Question by
sebi3110 · Dec 27, 2013 at 04:18 PM ·
c#2dmovementrigidbody.addforce
Jump with Spacebarhit. Longjump with Spacebar Hold
Hello all!
I'm currently trying to make a little "jump only" game.
My goal is that the player can perform short jumps in y and x direction (ergo a diagonaljump if you want) when the player hits the spacebar.
But I also want to let the player perform a longer jump when he holds the spacebar for let's say one second.
To sum it up:
Spacebar hit --> short jump (in x and y direction)
Spacebar pressed and hold --> long jump (in x and y direction)
I tried a lot, but the results don't satisfy me in any way...
That's what I have right now:
void Update()
{
//Gravity Control
////////////////////////////////////////////////
if(grounded)
{
Physics2D.gravity = new Vector3(0, -30, 0);
}
else
Physics2D.gravity = new Vector2(200, -30);
////////////////////////////////////////////////
///
///
if (!grounded)
{
jumpTime += Time.deltaTime;
anim.SetFloat ("jumpTime", jumpTime);
}
else if (grounded)
{
jumpTime = 0f;
}
//Jump Control
////////////////////////////////////////////////
///
if (grounded && Input.GetButton ("Jump"))
{
anim.SetBool ("Ground", false);
rigidbody2D.AddForce (new Vector2 (jumpForceX, jumpForceY));
if(Input.GetButtonDown ("Jump")&& jumpTime>0f)
rigidbody2D.AddForce (new Vector2 (20*jumpForceX, 20*jumpForceY));
}
}
Do you have an idea how I could tackle this? :)
Cheers!
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220613124949im_/https://answers.unity.com/themes/thub/images/avi.jpg)