The question is answered, right answer was accepted
Double Jump
Hello guys, I know alot of people asked to this, and I know there are alot of tutorials..but I really don't know how to came out.
So, I followed a tutorial, and now my character is moving and jumping; but what about if I want him to double jump? What can I do in this case? Here the code I'm using
//ANIMATIONS
//PLAYER DIRECTION
if (moveX < 0.0f && facingRight == false)
{
FlipPlayer();
}
else if (moveX > 0.0f && facingRight == true)
{
FlipPlayer();
}
//PHYSICS
gameObject.GetComponent<Rigidbody2D>().velocity = new Vector2(moveX * playerSpeed, gameObject.GetComponent<Rigidbody2D>().velocity.y);
}
void Jump()
{
//JUMPING CODE
GetComponent<Rigidbody2D>().AddForce (Vector2.up * playerJumpPower);
Thank you all for the help!
@tokyotora26 - You have two similar questions on double jumping, only 10 $$anonymous$$utes between them. Which one is correct?
This one, an error of $$anonymous$$e. Please forgive me :'(
Answer by eses · Sep 01, 2018 at 11:11 AM
Hi @tokyotora26 - This question has already been asked answered - just google it:
https://answers.unity.com/questions/643228/double-jump-2.html
I know I'm a kind of n00b, but it's really my very first time in ga$$anonymous$$g, so.. I've tried, the problem persist. As I said I 've checked alot of tutorials and discussions, but I really don't know how to match the codes correct with $$anonymous$$e... It's clear that I'm missing something, but I can't figure what is..
@tokyotora26 - "Please Help!" is not synonym for getting free code ins$$anonymous$$d doing the learning yourself. I'm in the same boat, maybe a step or two ahead.
You don't have to match any code with yours, you'll have to learn the basics of C#, Unity and coding in general. If you had, you'd notice what they did, ins$$anonymous$$d of blindly taking some piece of code you don't understand.
Oh well, never$$anonymous$$d then. I'll just do myself. I didn't asking for free codes at all anyway, I'm working hard with everything, this code too and not so "blindy" as you think. If I didn't tried myself many times maybe I probably not be here asking right now, what I needed it was just a hint. Thanks for nothing.
Answer by Rynbernsz · Sep 01, 2018 at 01:44 PM
What I did was:
add a jump counter.
And if player wasn't grounded and jumpCount is less than 1,
then call jump again
and jumpCount++.
Pretty simple once I figured it out. Try that and see what ya get. Hopefully I explained it clearly!
Thank you for the kind help, here what I did:
int jumpCount = 2;
and in //CONTROLS
if (isGrounded == false && jumpCount < 1)
{
Jump(); jumpCount++;
}
But I know i did a mistake for sure, because it's not working.. but at least I don't have errors of Script this time x'3
Hello, sorry for the late, I tried again from the start, but nothing seems to work... the function is this
void OnCollisionEnter2D(Collision2D col)
{
Debug.Log("Player has collided with" + col.collider.name);
if (col.gameObject.tag == "ground")
{
isGrounded = true;
jumps = 0;
anim.SetBool("IsJumping", false);
}
}
void Jumping()
{
if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Space))
{
if (isGrounded)
{
body.AddForce(Vector2.up * jumpforce, Force$$anonymous$$ode2D.Impulse);
jumps++;
anim.SetBool("IsJumping", true);
}
else
{
if (jumps < maxJumps)
{
body.AddForce(Vector2.up * jumpforce, Force$$anonymous$$ode2D.Impulse);
jumps++;
anim.SetBool("IsJumping", true);
}
Follow this Question
Related Questions
How to double jump? i just can jump once when it touch the ground.. help me guys =D 1 Answer
Double Jump in Fighting Game,Double Jump with Force 1 Answer
Player rotation always resets. 0 Answers
Can Someone Explain Why Message does not show Anything In Inspector 0 Answers
unity don't recognise the elements (0, 1, 2 etc...) what can i do ? 0 Answers