0 votes

Hi, I am learning Godot by following the tutorial to make a Platformer. I have a simple KinematicBody2D on top of StaticBody2D, however, I am not able to move the player in the X direction. The player is able to successfully jump in the Y direction but is unable to move in the X direction.

Below is the code for Player

extends KinematicBody2D

var velocity = Vector2.ZERO

func _physics_process(delta):
    velocity.y += 2

    if Input.is_action_pressed("ui_right"):
        velocity.x += 40
    elif Input.is_action_pressed("ui_left"):
        velocity.x += -40
    else:
        velocity.x = 0

    if Input.is_action_just_pressed("ui_up"):
        velocity.y = -10

    move_and_collide(velocity)

Link to the tutorial : https://www.youtube.com/watch?v=f3WGFwCduY0

Below is how my scene looks like
enter image description here

Help is much appreciated :)

Link to Project : https://drive.google.com/file/d/1HvcPcKmS3WRYml0fCYjMbNRTuhTWFCZB/view?usp=share_link

Link to Simulation Video
https://drive.google.com/file/d/1M2VAgJcUoAWlnJy8sIFa1glxlL7SzNzE/view?usp=share_link

Godot version 3.5
in Engine by (12 points)
edited by

Have you verified that the Input Map for the ui_right and ui_left actions are properly assigned? What happens if you add a print() statement inside each of the if blocks for those actions? Do you see them printed in the console when the keys are pressed?

Hi jgodfrey,

Thank you for replying. I have checked the console and the velocity is printed correctly in the debugger.

However, it appears that there is unnecessary friction applied when trying to move KinematicBody2D on the StaticBody2D.

You can find the simulation and project link as below:

Link to Project : https://drive.google.com/file/d/1HvcPcKmS3WRYml0fCYjMbNRTuhTWFCZB/view?usp=share_link

Link to Simulation Video
https://drive.google.com/file/d/1M2VAgJcUoAWlnJy8sIFa1glxlL7SzNzE/view?usp=share_link

1 Answer

0 votes

Its difficult to analyse without the rest of the code but there doesnt seem to be anything inherently wrong with the code above.

I would say the most likely cause is a capitalisation error or something similar. If your animation is called Ui_right instead of ui_right then the system will see them as different and nothing will happen in your code as no button is assigned to the lowercase version you have input.

by (3,009 points)

Hi, Gluon

Thank you for the reply.

I don't think there is any issue with the code. When I restarted the Godot application today, the movements were working. However, it seems there is unnecessary friction applied with moving the kinematicBody2D left or right.

You can find the video of my simulation as well as the Godot project below

Link to Project : https://drive.google.com/file/d/1HvcPcKmS3WRYml0fCYjMbNRTuhTWFCZB/view?usp=share_link

Link to Simulation Video
https://drive.google.com/file/d/1M2VAgJcUoAWlnJy8sIFa1glxlL7SzNzE/view?usp=share_link

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.