Actually the name is the problem. The code is as follows:

private bool isGrounded = true; private bool isAir = false; private void Update() { if (isGrounded) State = CharState.Idle; if (isGrounded && Input.GetButtonDown("Jump")) Jump(); //if (isAir && Input.GetButtonDown("Jump")) DoubleJump(); //if (isGrounded && Input.GetButtonDown("Jump + Jump")) DoubleJump(); } private void Jump() { if (Input.GetButtonDown("Jump")) DoubleJump(); else { rigidbody.AddForce(transform.up * jumpForce, ForceMode2D.Impulse); } //rigidbody.AddForce(transform.up * jumpForce, ForceMode2D.Impulse); //isAir = true; isGrounded = false; } private void DoubleJump() { rigidbody.AddForce(2 * transform.up * jumpForce, ForceMode2D.Impulse); //isAir = false; //isGrounded = true; } 

The last attempt was like this, but when activating the Jump method, the character immediately goes into the DoubleJump, although it should be wrong.

  • And what do you want, so that the jump would work only from the jump from the ground. Or that in the fall you could bend down again? - Aqua
  • Take a closer look, if the "Jump" button is pressed in the current frame, the Jump() method is executed, in which exactly the same check leads to the DoubleJump() call. It turns out at some point you click on the "Jump" button, so both conditions are met and the code proceeds to DoubleJump() . - Mikhail Deyman
  • one
    The very idea of ​​such an implementation is not the most correct, stateless machines are indispensable. - RiotBr3aker
  • I would like to jump at any point of the jump. Yes, I understand that this code works in one frame, but alas, I do not know how to distinguish it. Knowledge is not enough for other implementations. - Alexander

1 answer 1

I agree with the question about the fact that this is not a very correct implementation and it is better to make a state machine. It is not very difficult and I recommend to google examples of the simplest state machines (the benefit of them in bulk). However, if you still want to use this approach, then please try using this code:

  private bool isGrounded = true; private bool canDoubleJump = true; private void Update() { if (Input.GetButtonDown("Jump"))//если нажата кнопка Jump { if (isGrounded)//проверяем, находится ли персонаж на земле { Jump();//если да, то прыгаем "первый" прыжок } else if (canDoubleJump)//если персонаж не приземлён и может прыгнуть ещё раз, то прыгаем "второй" прыжок { DoubleJump(); } //если персонаж не приземлён и больше не может прыгать, то при нажатии кнопки Jump ничего не произойдёт } } private void Jump() { isGrounded = false;//устанавливаем, что персонаж не приземлён canDoubleJump = true;//устанавливаем, что персонаж может прыгнуть "второй" раз rigidbody.AddForce(transform.up * jumpForce, ForceMode2D.Impulse); } private void DoubleJump() { canDoubleJump = false;//устанавливаем, что персонаж не может прыгнуть "второй" раз, пока не прыгнет "первый" раз rigidbody.AddForce(2 * transform.up * jumpForce, ForceMode2D.Impulse); } //этот метод должен вызываться при приземлении персонажа private void Landing() { isGrounded = true;//устанавливаем, что персонаж приземлён canDoubleJump = false;//устанавливаем, что персонаж не может прыгнуть "второй" раз, пока не прыгнет "первый" } 
  • State = CharState.DoubleJump; //если надо State = CharState.DoubleJump; //если надо - what is it and why is it needed in this code? - RiotBr3aker
  • @RiotBr3aker apparently it is just some rudiments for the state machine. Made it so as to show where, in my opinion, it is better to assign steits. Let me remind you, State = CharState.Idle in the code from the question is in Update() . - Mikhail Deyman
  • Then it is not very correct, it is necessary to separate the behavior depending on the current state and the change of steits, to interfere with them is a thankless job. - RiotBr3aker
  • Yes, this code works correctly, but if I quickly press the spacebar several times, the character flies away insanely high. How can this be fixed? - Alexander
  • @RiotBr3aker, the author has already written that "Knowledge is not enough for other implementations", so it may not be worth much to complicate its code, otherwise it simply will not be able to work with it normally. In the end, he did not ask "How to create a simple state machine," but asked for help to deal with his small problem. - Mikhail Deyman