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

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 (426 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 Frequently asked questions and 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 [email protected] with your username.