0

I'm trying to implement a double jump mechanic in my Godot game using C# and I can get him to jump twice but I can't figure out how to limit the jumps to only 2 times. I tried using a jumpCount int but it was to no avail. I have not been able to figure out the best way to implement this mechanic I am fairly new to C# and I know that pretty much my entire player script is if statements but that is the best way I could figure to get it working.

 using Godot;
 using System;

 public class player : KinematicBody2D
{
const int jumpForce = 125;
const float gravity = 200.0f;
const int moveSpeed = 80;
const float maxFallspeed = 200.0f;
const float acceleration = 10;
float jumpCount = 2;

public Vector2 velocity = new Vector2(0, -1);

public override void _PhysicsProcess(float delta)
{
    var ap = GetNode("AnimationPlayer") as AnimationPlayer;
    var sprite = GetNode("playerTex") as Sprite;

    velocity.y += delta * gravity;
    velocity.x = Mathf.Clamp(velocity.x, -moveSpeed, moveSpeed);



    if (velocity.y > maxFallspeed)
    {
        velocity.y = maxFallspeed;
    }

    if (Input.IsActionPressed("right"))
    {
        sprite.FlipH = false;
        velocity.x += acceleration;
        ap.Play("run");
    }

    else if (Input.IsActionPressed("left"))
    {
        sprite.FlipH = true;
        velocity.x -= acceleration;
        ap.Play("run");
    }

    else
    {
        velocity.x = 0;
        ap.Play("idle");
    }

    if (Input.IsActionJustPressed("jump") && jumpCount < 2)
    {
        jumpCount += 1;
        velocity.y = -jumpForce;
    }

    if (Input.IsActionPressed("jump") && IsOnFloor())
    {
        jumpCount = 0;
        velocity.y = -jumpForce;
    }

    if (velocity.y < 0 && !IsOnFloor())
    {
        ap.Play("fall");
    }

    if (velocity.y > 0 && !IsOnFloor())
    {
        ap.Play("jump");
    }

    MoveAndSlide(velocity, new Vector2(0, -1));
}

}
  • SHouldn't that be `if (Input.IsActionJustPressed("jump") && IsOnFloor())` instead of `if (Input.IsActionPressed("jump") && IsOnFloor())`? – Rodrigo Rodrigues Jan 25 '21 at 01:33
  • If you leave it as (Input.IsActionPressed("jump") && IsOnFloor()) then holding down the space bar will cause him to jump twice I used (Input.IsActionJustPressed("jump") && IsOnFloor()) so the player would have to hit the space bar again to initiate a second jump. – Joshua Duane Jan 25 '21 at 01:37
  • 1
    I kind of answered my own question. I had to limit the jumpCount to 2 but use if (Input.IsActionJustPressed("jump") && jumpCount < 1 && !IsOnFloor()) instead of if (Input.IsActionJustPressed("jump") && jumpCount < 2 && !IsOnFloor()) cause the jumpCount += 1; was making him jump a total of 3 times while in the air. – Joshua Duane Jan 25 '21 at 01:42
  • If you found an answer yourself, please post it as an answer and mark it as accepted. – Zer0 Jan 25 '21 at 02:46

0 Answers0