Player moves weird

Godot Version

Godot Engine v4.5.stable.steam (c) 2007-present Juan Linietsky, Ariel Manzur & Godot Contributors.

Question

so my player is moving but not correctly? the left button is moving it at increased velocity. the player also doesnt stop moving when button was pressed?
extends CharacterBody2D

@export var speed = 100

func _physics_process(delta):
var direction = Input.get_vector(“walk_left”, “walk_right”, “walk_up”, “walk_down”)

if Input.is_action_pressed("walk_up"):
	velocity.y -= 1
if Input.is_action_pressed("walk_right"):
	velocity.x += 1
if Input.is_action_pressed("walk_down"):
	velocity.y += 1
if Input.is_action_pressed("walk_left"):
	velocity.x -= 1
	
	velocity = direction * speed

move_and_slide()

The line velocity = direction * speed is inside the if-statement if Input.is_action_pressed("walk_left"), so it’s only executed if the left button was pressed. Unindent the line by one tab and it should work.
Also you can remove all those if-statements because the velocity is overridden directly after anyway by the aforementioned line.

@export var speed = 100

func _physics_process(delta):
	var direction = Input.get_vector(“walk_left”, “walk_right”, “walk_up”, “walk_down”)

	#if Input.is_action_pressed("walk_up"):
		#velocity.y -= 1
	#if Input.is_action_pressed("walk_right"):
		#velocity.x += 1
	#if Input.is_action_pressed("walk_down"):
		#velocity.y += 1
	#if Input.is_action_pressed("walk_left"):
		#velocity.x -= 1

	velocity = direction * speed

	move_and_slide()

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