I'm trying to game a game using the Godot Engine but I'm stuck at the beginning! I can't make my KinematicBody2D move!

This is my Player.GD script

extends KinematicBody2D var velocity = Vector2.ZERO var move_speed = 480 var gravity = 1200 var jump_force = -720 var right = Input.is_action_pressed("move_right") var left = Input.is_action_pressed("move_left") var jump = Input.is_action_pressed("jump") func _ready(): pass func _physics_process(_delta): var move_direction = int(right) - int(left) velocity.x = move_speed * move_direction move_and_collide(velocity) 

Can someone, please, help me?

1 Answer

All this code will run when the KinematicBody2D is initialized:

var velocity = Vector2.ZERO var move_speed = 480 var gravity = 1200 var jump_force = -720 var right = Input.is_action_pressed("move_right") var left = Input.is_action_pressed("move_left") var jump = Input.is_action_pressed("jump") 

In consequence, it will not be taking input in real time. Instead you want the last three lines here:

func _physics_process(_delta): var right = Input.is_action_pressed("move_right") var left = Input.is_action_pressed("move_left") var jump = Input.is_action_pressed("jump") # … 

Those are boolean, by the way. You can get a float from 0.0 to 1.0 if you use Input.get_action_strength instead. Which will also let your code ready for analog input.


I also want to point out that move_and_collide does not take a velocity, but a displacement vector. So to call it correctly, you want to multiply the velocity by delta:

func _physics_process(delta): # … move_and_collide(velocity * delta) 

Or use move_and_slide, which does take a velocity. By the way, the up parameter that move_and_slide takes is to discern between floor, ceiling, and walls. Without, everything is considered a wall.

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.