Chracter Movement

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

Hello, I want to movement my character. But I don’t know where is the mistake Please help me! I want to use W,S,D,A What can I do ?

Script:

extends KinematicBody2D

const SPEED = 70

var movedir = Vector2 (0,0)

func _physics_process(delta):
	controls_loop()
	movement_loop()

func controls_loop():
	var LEFT = Input. is_action_just_pressed("ui_left")
	var RIGHT = Input. is_action_just_pressed("ui_rıght")
	var UP  = Input. is_action_just_pressed("ui_up")
	var DOWN = Input. is_action_just_pressed("ui_down")

func movement_loop():
		var motion = movedir.normalized() * SPEED
		move_and_slide(motion,Vector2(0,0))
:bust_in_silhouette: Reply From: kidscancode

Note: I edited your post. Please use the “Code Sample” button when pasting code so that the formatting isn’t lost.

There are a few problems here:

  1. You have a variable called movedir that you’re using to move, but that variable never changes.

  2. In controls_loop() you’re capturing the input actions into variables that you never do anything with.

  3. Using is_action_just_pressed() will only count during the frame that you press the key. You’ll have to keep releasing and pressing it again. This should be is_action_pressed() if you want continuous movement.

  4. The actions you’re using (‘ui_right’, etc.) are the built-in defaults, and are mapped to arrow keys. If you want to use WASD, you’ll have to open Project Settings → Input Map and create your own actions with those inputs.

See the following tutorial for examples of how to do basic 2D movement: