I've tried everything, nothing works with my movement!

Godot Version 4.3v
extends CharacterBody2D

const SPEED = 300.0
const ACCEL = 2.0

var input: Vector2 = Vector2.ZERO

func get_input():
input.x = Input.get_action_strength(“left”) - Input.get_action_strength(“right”)
input.y = Input.get_action_strength(“up”) - Input.get_action_strength(“down”)
return input.normalized()

func _physics_process(delta):
var playerInput = get_input()

velocity = playerInput * SPEED

move_and_slide()

Question

i’m very new to coding and tried without a tutorial for the first time, but i’m so stuck. My inputs all match up from my input map but my character still wont move. please help!!

Did you add the script to your player node?

Is your code indented in each code block or is it all aligned to the left like your text?

Yes, it’s connected to my CharacterBody2d Node

Yeah it’s all indented correctly :frowning:

extends CharacterBody2D

const SPEED = 300.0
const ACCEL = 2.0

func get_input() → Vector2:
var input = Vector2.ZERO
# Changed to get_axis for smoother input handling
input.x = Input.get_axis(“left”, “right”)
input.y = Input.get_axis(“up”, “down”)
return input.normalized()

func _physics_process(delta: float) → void:
# Get the input direction
var direction = get_input()

# If there's input, move in that direction
if direction != Vector2.ZERO:
    velocity = direction * SPEED
else:
    # Optional: Add friction when no input is detected
    velocity = velocity.move_toward(Vector2.ZERO, SPEED * delta)

# Apply the movement
move_and_slide()

I’m a beginner coder but can’t see anything wrong. Can you post all the code in a preformatted text block so we can see it?

(I’ve cut and paste a CharacterBody3D script template below)

extends CharacterBody3D


const SPEED = 5.0
const JUMP_VELOCITY = 4.5


func _physics_process(delta):
	# Add the gravity.
	if not is_on_floor():
		velocity += get_gravity() * delta

	# Handle jump.
	if Input.is_action_just_pressed("ui_accept") and is_on_floor():
		velocity.y = JUMP_VELOCITY

	# Get the input direction and handle the movement/deceleration.
	# As good practice, you should replace UI actions with custom gameplay actions.
	var input_dir = Input.get_vector("ui_left", "ui_right", "ui_up", "ui_down")
	var direction = (transform.basis * Vector3(input_dir.x, 0, input_dir.y)).normalized()
	if direction:
		velocity.x = direction.x * SPEED
		velocity.z = direction.z * SPEED
	else:
		velocity.x = move_toward(velocity.x, 0, SPEED)
		velocity.z = move_toward(velocity.z, 0, SPEED)

	move_and_slide()

I copy-pasted your code and two things:

Godot editor complained about these “ you used as invalid character, are you sure editor is not showing any errors? Replacing the “ in code the character moved but the inputs are inversed

Are you totally sure your inputs names are correct? Try replacing them by "ui_up", "ui_down", "ui_left" and "ui_right" and use the arrows to move the character.


Also your code can be simplified to this (if your input map is correct or use the ui_ versions):

extends CharacterBody2D

const SPEED = 300.0
const ACCEL = 2.0


func _physics_process(delta):
	velocity = Input.get_vector("left", "right", "up", "down") * SPEED
	move_and_slide()
1 Like

This helped thank you so much!!

If that worked probably you created the input but didn’t binded any key to activate these inputs

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