0
\$\begingroup\$

I'm new to Godot (and C# in general, but I use that because that's the language I'm learning in school, and I don't want to confuse myself with GDscript), and I tried to code a 2D player character (nothing too big, just moving, sprinting, jumping, and ground pounding), but when I got to the jumping mechanics, instead of player jumping, it just teleported upwards, and then glided downwards. And the ground pound just teleported me to the ground.

Here's the code:

using Godot;
using System;

public class Player : KinematicBody2D
{
    private float speed = 600;
    private float gravity = 500;
    private float jumpForce = -5000;
    //Basic variables for stuff

    public override void _PhysicsProcess(float delta)
    {
        var move = Vector2.Zero;
        var animatedSprite = GetNode<AnimatedSprite>("AnimatedSprite");
        move.y = gravity;
        //Other variables
        
        if (Input.IsActionJustPressed("jump") && IsOnFloor())
        {
            move.y = jumpForce;
        }
        //Jumping
        
        if (Input.IsActionPressed("left"))
        {
            move.x = -speed;
            animatedSprite.Play("walk");
            animatedSprite.FlipH = true;
            
            if (Input.IsActionPressed("sprint"))
            {
                move.x = (-speed) + (-400);
            }
        }
        if (Input.IsActionPressed("right"))
        {
            move.x = speed;
            animatedSprite.Play("walk");
            animatedSprite.FlipH = false;
            
            if (Input.IsActionPressed("sprint"))
            {
                move.x = speed + 400;
            }
        }
                //Movement and sprint

        if(Input.IsActionJustPressed("pound"))
        {
            move.y = -jumpForce;
        }
        //Groundpound
        
        MoveAndSlide(move, Vector2.Up);
    }
}

The other thing with jumping is that I need a really big number to lift the player from the ground, so that jumpForce variable in the code is barely enough to lift the character.

A tried to tweak it a little bit, changed some numbers (I thought that maybe I just screwed up the variables), but that don't worked.

\$\endgroup\$

1 Answer 1

2
\$\begingroup\$

I don't have a C# environment set up so I can't test my solution, but I did port your code to GDScript and I think I can point out some errors.

The reason your character teleports is because it has no implementation of velocity. To fix this, make move a member variable of Player. Then, instead of setting move with =, add to it with +=. Lastly, be aware MoveAndSlide returns a a modified velocity if you collide with something, so you should change the line to say move = MoveAndSlide(move, Vector2.Up);. Lastly, smaller numbers can be used for the physics values since they will accumulate over time.

I believe these modifications would look like this:

using Godot;
using System;

public class Player : KinematicBody2D
{
    private float speed = 10;
    private float gravity = 10;
    private float jumpForce = -300;
    private Vector2 move = Vector2.Zero;
    //Basic variables for stuff

    public override void _PhysicsProcess(float delta)
    {
        var animatedSprite = GetNode<AnimatedSprite>("AnimatedSprite");
        move.y += gravity;
        //Other variables
        
        if (Input.IsActionJustPressed("jump") && IsOnFloor())
        {
            move.y += jumpForce;
        }
        //Jumping
        
        if (Input.IsActionPressed("left"))
        {
            move.x += -speed;
            animatedSprite.Play("walk");
            animatedSprite.FlipH = true;
            
            if (Input.IsActionPressed("sprint"))
            {
                move.x += (-speed) + (-400);
            }
        }
        if (Input.IsActionPressed("right"))
        {
            move.x += speed;
            animatedSprite.Play("walk");
            animatedSprite.FlipH = false;
            
            if (Input.IsActionPressed("sprint"))
            {
                move.x += speed + 400;
            }
        }
                //Movement and sprint

        if(Input.IsActionJustPressed("pound"))
        {
            move.y += -jumpForce;
        }
        //Groundpound
        
        move = MoveAndSlide(move, Vector2.Up);
    }
}

If this works, keep in mind your character will slide after moving because there is no code to slow down when you let go of the movement keys.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .