0 votes

I'm new to Godot (and computer programming in general), and to sharpen my skills I decided to try and make a simply 2D platforming game. The player is a KinematicBody2D. I made some tile sprites for a TileMap, and gave each tile the correct collision boxes (I think). I coded my player to move and jump with this:

#gravity and jumping
if is_on_floor() == true:

    if Input.is_action_just_pressed("ui_up"):
        velocity.y = JUMP_POWER
else:
    velocity.y += GRAVITY
if is_on_ceiling() == true:

    velocity.y += 30

#collision testing

if is_on_floor() == true:
    if is_on_wall() == true:
        print("wall and floor")
    else:
        print("floor")
elif is_on_wall() == true:
    print("wall")

#left and right movement
if Input.is_action_pressed("ui_left") == true:
    velocity.x = -SPEED
elif Input.is_action_pressed("ui_right") == true:
    velocity.x = SPEED
else:
    velocity.x = 0

Extending KinematicBody2D, the code above is the contents of a function that I ran inside _physics_process() , and then I used `moveandslide(velocity, Vector2(0,-1), true)' to make everything work.

The problem I have is that when I'm controlling my player and I walk into walls, it seems to sort of glitch into the wall at an angle downward. When this happens, the jumping functions are a little hit or miss. Now, you may have noticed my that I've got a section of code above that I put a comment above calling it "collision testing". It'll just test if its colliding properly. Running this code while I'm colliding into the wall and making it glitch into the corner gives me this console output:

wall and floor
wall
wall and floor
wall
wall and floor

... and so on. This tells me that my jumping mechanics are bad in the corner because Its only sometimes detecting my player as touching the floor. There's something wrong here, and I hope its something wrong with my code. Can anyone help me make collisions work smoother? Here's a few images of my setup:

Where I ran into the wall

Starting position

TLDR my collision is wacky, and when I walk into a corner it only detects that I'm colliding with both the wall and the floor every other frame. Every frame in between, it just thinks I'm colliding with the wall and nothing else. My jumping mechanics are tied to whether or not the player is on the ground, so when this happens its literally a 50-50 chance of working. Help?

asked Mar 30 in Engine by Taurenzine (16 points)

1 Answer

0 votes

Ok so I fixed it but I have no Idea how what I did made it work so... I would still like help. This is what I did:

I changed my code from this:

if is_on_floor() == true:
      if Input.is_action_pressed("ui_up"):
            velocity.y = JUMP_POWER
else:
      velocity.y += GRAVITY

to this:

if is_on_floor() and Input.is_action_pressed("ui_up"):
      velocity.y = JUMP_POWER
else:
      velocity.y += GRAVITY

and somehow that fixed it. My collision checker code only spits out that I'm on both the floor and wall when I'm in a corner. To me, this makes absolutely no sense though, because Logically, the outcomes should be the same. But clearly I'm missing something. Maybe it has to do with the fact that is_on_floor() only updates when calling move_and_slide() but even then, I call move_and_slide() every frame, so I don't know why it wouldn't update properly...

answered Mar 30 by Taurenzine (16 points)
edited Mar 30 by Taurenzine
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.