Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
547 views
in Technique[技术] by (71.8m points)

2d - Trying to implement a double jump function in C# that limits to only 2 jumps in Godot game engine

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));
}

}
question from:https://stackoverflow.com/questions/65877635/trying-to-implement-a-double-jump-function-in-c-sharp-that-limits-to-only-2-jump

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...