#Player Movement Coding Help Needed

11 messages · Page 1 of 1 (latest)

wicked sigil
#

There are no stated errors, but the game won't react at all to my key inputs. Below is my code. I want to use the key input system instead of the map. Feel free to tell me what has worked for you. Also, there's this blue bent arrow next to the starting function line, which I'd like to know how to remove.

extends CharacterBody2D
var speed = 100
var direction_x = 0
var stopped = 1

func _physics_process(delta):
if stopped == 1:
direction_x = 0
if Input.is_key_pressed(KEY_A):
stopped = 0
direction_x = -1
else:
stopped = 1
if Input.is_key_pressed(KEY_D):
stopped = 0
direction_x = 1
else:
stopped = 1
velocity.x = direction_x * speed * delta
move_and_slide()

wide oyster
#

The blue arrow indicates that it's a built-in function (_process, _ready, _physics_process, anything that comes default for node) but besides that there's some issues with your code

do you have any UI layers that may be eating the input?
and your script behaves not how you might expect it to (if you're pressing A but not D, stopped is still set to 1 even though you're pressing the A key)

#

and are you sure the player is not moving? The physics delta is a really small value so your player could just be moving at a very slow speed

#

for reference I tried your script and it works

wicked sigil
#
  1. Not that i know of
  2. As it is, pressing either key does nothing.
  3. I'm pretty sure. Before it was moving fine, but I'm not sure what I changed that made it stop working.
wide oyster
#

place a breakpoint on the move_and_slide, see if it even runs it

plush bluff
#

quite often when someone has a problem of a character not moving it's just the sprite not being properly attached to it as a child node, meaning it does actually move, you just can't see it.
I of course can't tell if this is also the case here.

wicked sigil
wicked sigil
wicked sigil
#

i got it to work with
extends CharacterBody2D
var xdir = 0
func _physics_process(delta):
xdir = 0
if Input.is_key_pressed(KEY_A):
xdir = -1
if Input.is_key_pressed(KEY_D):
xdir = 1
velocity.x = xdir*400
move_and_slide()

plush bluff
#

oh right, delta gets already considered when calling move and slide, so you did it twice, which would slow it down greatly, but it should still have moved.