- Home /
How do i incorporate a double jump ingame?
I have a functioning 2D "sphere" ingame object and i have it resting on a cube object. I can successfully move it left and right, and jump an infinite amount of times in the directions i press. Im having an issue trying to figure out how to put a limit on the jump amount though. I want the sphere to only be able to double jump before it reaches some sort of cap and reached the ground, instead of being able to basically fly with how many jumps it has. Heres my snippet of code so far:
void Update ()
{
//the rotation gets the value of the input (on the horizontal axis) times the
//value of the rotation speed
float rotation = Input.GetAxis ("Horizontal") * rotationSpeed;
//this rotation happens independantly as the frames refresh
rotation *= Time.deltaTime;
//sets a torque like movement to the rigidbody2d of the sphere, by minus 1
//for opposite torqing direction, making right right and left left
rigidbody2D.AddTorque (-1 * rotation);
if (Input.GetKeyDown (KeyCode.UpArrow) && isFalling == false)
{
rigidbody2D.velocity = new Vector2 (rigidbody2D.velocity.x, rotation);
rigidbody2D.velocity = new Vector2 (rigidbody2D.velocity.y, jumpHeight);
isFalling = true;
}
else
{
isFalling = false;
}
}
void OnCollisionStay ()
{
isFalling = false;
}
Im still beginning, and im a bit all over the place since i tried multiple solutions but didnt delete any of them. Thanks in advance.
You need to implement a check to see if you character is on the ground. Raycasting is a popular way to do this. Others prefer triggers.
Velocity is dangerous as a projectile under gravity will have zero velocity at the top of its arc. About as far away from the ground as you can get.
Gotcha, so if i simply look up some Raycasting and/or Trigger solutions I should be on the right track. Gonna have to keep these things in $$anonymous$$d then.
Answer by jusabian · Jul 31, 2014 at 08:11 AM
Hi,
I did also a doublejump check with the raycasting method. Simply check with a raycast if the ground is near and let the player jump if the ground is in range. The raycasting could be in a coroutine which triggers a bool variable to allow the jumping, or simply check before the jumping when the player presses the jump button.
Hope it helps,
Jusabian
Hey, thanks for the added advice, will certainly look into those techniques. Gonna test them out tomorrow and hopefully come up with a new batch of code to smooth out the issue. If the thread is still open by that time, i'll paste a code sample.
Your answer
Follow this Question
Related Questions
C# Water Rotation 0 Answers
Problem with joints 2 Answers
Sucking a zero-gravity ship into a black hole. 1 Answer
Script calling onCollisionEnter2D and onTriggerEnter2D while disabled 1 Answer
2d game platform physics 1 Answer