Kinematic body moves in Y direction but does not move in X direction

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

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

Help is much appreciated :slight_smile:

Link to Project : godotOne.zip - Google Drive

Link to Simulation Video

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?

jgodfrey | 2022-11-06 15:18

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 : godotOne.zip - Google Drive

Link to Simulation Video
godot_move_recording.mov - Google Drive

tdhslayer | 2022-11-07 03:47

:bust_in_silhouette: Reply From: Gluon

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.

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 : godotOne.zip - Google Drive

Link to Simulation Video
godot_move_recording.mov - Google Drive

tdhslayer | 2022-11-07 03:43