Jumping problem.

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By rpggeek

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

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

ramazan | 2022-01-31 22:07

:bust_in_silhouette: Reply From: SteveSmith

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.