0 votes

Hey guys, long term Unity user here.
When i press the space key, my player doesn't start to jump.

My code here:

using Godot;
using System;

public class player : KinematicBody2D
{
    private float speed = 50f;
    private float speedAccelerator = 1f;
    private float maxSpeed = 300f;
    private float gravity = 5500f;
    private Vector2 velocity;
    private Sprite spriteNode;
    private AnimationPlayer anima;
    private bool spaceKeyPressed = false;
    private KinematicCollision2D kinemCol;
    private bool jumped = false;
    public override void _Ready()
    {
        spriteNode = (Sprite)GetNode("Sprite");
        anima = (AnimationPlayer)GetNode("Sprite/AnimationPlayer");
    }

public override void _Process(float delta)
{ 
    velocity.y += gravity;
    
    if(Input.IsKeyPressed(((int)KeyList.D)))
    {
        MoveAnim();
        FlipRight();
        if(velocity.x < maxSpeed) velocity.x += speed;
    }
    else if(Input.IsKeyPressed(((int)KeyList.A)))
    {
        MoveAnim();
        FlipLeft();
        if(velocity.x > -maxSpeed) velocity.x -= speed;
    }
    else{
        IdleAnim();
        Decceleration();
    }
    if(Input.IsActionJustPressed("key_press_SPACE"))
    {
        if(IsOnFloor()){
            Jump();
        }
    }

    velocity = MoveAndSlide(velocity, new Vector2(0,-1));
    GD.Print(velocity);

}

private void MoveAnim(){
    if(!anima.CurrentAnimation.Equals("Move")){
            anima.Play("Move");
        }
}
private void FlipLeft(){
if(spriteNode.FlipH.Equals(false)){
            spriteNode.FlipH = true;
        }
}
private void FlipRight(){
if(spriteNode.FlipH.Equals(true)){
            spriteNode.FlipH = false;
        }
}
private void Jump()
{
    velocity.y = 50000f;
}
private void IdleAnim(){
    if(anima.CurrentAnimation != "Idle"){
            anima.Play("Idle");
        }
}
private void Decceleration(){
    if(velocity.x > 0f)
    {
        velocity.x -= 750f;
        if(velocity.x < 0f) velocity.x = 0f;
    }
    else if(velocity.x < 0)
    {
        velocity.x += 750f;
        if(velocity.x > 0f) velocity.x = 0f;
    }
}
}

Godot version 3.4.2
in Engine by (37 points)

Add "if Jump != true" here.
true if "jump" not equal.
Because here idle is running. I think "idle" works when you press "Jump".
I don't know C#.
(sory i use Google translate)

}
else{
    IdleAnim();
    Decceleration();

Well, I don't know what's wrong with your code (although I'm guessing it's got something to do with IsOnFloor()), I can tell you that it would be much easier to help you if you added comments to your code.

edit:
Another thing that would be helpful would be if you print some of the variables at some points. Also, it doesn't seem like you're changing the position anywhere. Did you test the other movement, or just jumping?

1 Answer

0 votes

The line should be velocity.y = -50000f;

Since you're adding the gravity, it seems that to go up, you want to subtract from the y velocity.

by (28 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.