My character doesn't move

Godot Version

v3.5.3

Question

Hi
I just started working Godot and I started with youtube tutorials, I have followed everything to details but my character doesn’t move at all.

this is the code:
extends KinematicBody2D

var v: Vector2=Vector2()
var direct: Vector2=Vector2()

func read_input():
v=Vector2()

if Input.is_action_pressed("up"):
	v.y -=1
	direct=Vector2(0, -1)

if Input.is_action_pressed("down"):
	v.y+=1
	direct=Vector2(0, 1)

if Input.is_action_pressed("left"):
	v.x-=1
	direct=Vector2(-1, 0)
	
if Input.is_action_pressed("right"):
	v.x+=1
	direct=Vector2(1, 0)
	
v=v.normalized()
v=move_and_slide(v*200)

func physics_process(delta):
read_input()

Also i have added the script to the player.

Have you defined the InputMap actions you’re referencing?

“up”, “down”, “left”, and “right” don’t exist by default. You should define these manually under Project Settings → Input Map

yes i have

Even now I changed the binding keys to something else but nothing happened

And just to confirm, the code does run? No errors?

I would start by making sure your input is actually working. This is most easily done with a breakpoint, or a simple print(v) before you call move_and_slide(). Ensure v is actually being set the expected values when you try to move.

Also, there was some issues with the formatting in the code you posted. Ensure the code is indented as follows:

extends KinematicBody2D

var v: Vector2=Vector2()
var direct: Vector2=Vector2()

func read_input():
	v=Vector2()

	if Input.is_action_pressed("up"):
		v.y -=1
		direct=Vector2(0, -1)
	
	if Input.is_action_pressed("down"):
		v.y+=1
		direct=Vector2(0, 1)
	
	if Input.is_action_pressed("left"):
		v.x-=1
		direct=Vector2(-1, 0)
		
	if Input.is_action_pressed("right"):
		v.x+=1
		direct=Vector2(1, 0)
	
	print(v) # Debug print

	v=v.normalized()
	v=move_and_slide(v*200)


func physics_process(delta):
	read_input()

You’re welcome to upload your project files, and I can take a closer look. It’s a bit unclear where the issue is otherwise.

1 Like

where can i upload the files? cuz this is giving me the error for no authorized files. AND THANK YOU… i thought im losing my mind… thank god its not the code

Ah, shame. It looks like you’re right! I just tried myself and got the same issue.

In that case, Google Drive, OneDrive, or Github is perfectly fine with me. Not sure if there’s a recommendation/rules anywhere in the forums.

1 Like

Are you using Godot 4 version? move_and_slide() has no parameters now. It moves based on the velocity parameter, so you should use the variable parameter velocity.

extends KinematicBody2D

var direct: Vector2=Vector2()

func read_input():
	velocity=Vector2()

	if Input.is_action_pressed("up"):
		velocity.y -=1
		direct=Vector2(0, -1)
	
	if Input.is_action_pressed("down"):
		velocity.y+=1
		direct=Vector2(0, 1)
	
	if Input.is_action_pressed("left"):
		velocity.x-=1
		direct=Vector2(-1, 0)
		
	if Input.is_action_pressed("right"):
		velocity.x+=1
		direct=Vector2(1, 0)
	
	velocity =velocity.normalized()
	move_and_slide()


func physics_process(delta):
	read_input()

no i use v3.5.3

1 Like

@kmgghst
The best way to upload code (if on Windows) is to go to the project folder that contains everything and right-click on the folder, go to Send To and select the .zip option.

Then you can upload it Google Drive (or similar cloud storage) and share the link to the Zip file itself.

1 Like

i sent it for you via gmail, and again thank you

Got the files, it was a tiny typo which I competely missed!

It should be _physics_process(delta) instead of physics_process(delta).

From the docs:

Notice how _process() , like _init() , starts with a leading underscore. By convention, Godot’s virtual functions, that is to say, built-in functions you can override to communicate with the engine , start with an underscore.

1 Like

thank you mate…thank you

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.