Why can I only move backwards?

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

Hello there,

I’m new to the Godot community and stil learning. I have written a third person player script following a tutorial. When testing only the defined key for moving backwards(s) works. All other directions don’t seem to do anything at all. Why is that?
Here is my code:

extends KinematicBody

#Player speed control
export var speed : float = 20

#Player accelaration control
export var accelaration : float = 15 

#Player air_accelaration control 
export var air_acceleration : float = 5

#gravity control(rate per second at which the character falls)
export var gravity : float = 0.98

#maximum falling speed control
export var max_terminal_velocity : float = 54

#jump force control 
export var jump_power : float = 20

#mouse sensitivity control 
export(float, 0.1, 1) var mouse_sensitivity : float = 0.3

#min and max camera movement limit 
export(float, -90, 0) var min_pitch : float = -90
export(float, 0, 90) var max_pitch : float = 90

#Player current speed
var velocity : Vector3
var y_Velocity : float 

#remember element references 
onready var camera_pivot = $CameraPivot
onready var camera = $CameraPivot/CameraBoom/PlayerCamera

#func _ready() gets called after elements have loaded
func _ready():
	#Capture mouse input and hide cursor 
	Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)

#func _process(delta) gets called every frame	
#delta stores time elapsed since last call
func _process(delta):
	#if ui_cancel(default: ESC) button pressed -
	if Input.is_action_just_pressed("ui_cancel"):
		#-stop capture mouse and make cursor visible
		Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE)

#func _input(event) gets called with every input		
func _input(event):
	#if mouse is moved 
	if event is InputEventMouseMotion:
		#change "Player" y-rotation to relative x-movement of mouse * the mouse sensitivity
		rotation_degrees.y -= event.relative.x * mouse_sensitivity
		#change "camera_pivot" x-rotation to realtive y-movement of mouse * the mouse sensitivity
		camera_pivot.rotation_degrees.x -= event.relative.y * mouse_sensitivity
		#limit camera_pivot x-rotation 
		camera_pivot.rotation_degrees.x = clamp(camera_pivot.rotation_degrees.x, min_pitch, max_pitch)

#func _physics_process(delta) gets called 60 times per second (default value)
#delta stores the time elapsed since last call
func _physics_process(delta):
	#calculate movement for player
	movement_calc(delta)

#func movement_process(delta) gets called with every call of _physics_process
#delta stores time elapsed since last call of _physics_process
func movement_calc(delta):
	#Player direction control 
	var direction = Vector3()
	#while "move_forward"(default: W) is pressed
	if Input.is_action_pressed("move_forward"):
		#change direction in negative z
		direction -= transform.basis.z
	#while "move_backward"(default: S) is pressed
	if Input.is_action_pressed("move_backward"):
		#change direction in positive z
		direction += transform.basis.z 
	#while "move_left"(default: A) is pressed
	if Input.is_action_pressed("move_left"):
		#change direction in negative x
		direction -= transform.basis.x
	#while "move_right"(default: D) is pressed
	if Input.is_action_pressed("move_right"):
		#change direction in positive x 
		direction += transform.basis.x
	#normalize Vector 
	direction = direction.normalized() 
	
	#calculate velocity 
	var accel = accelaration if is_on_floor() else air_acceleration
	velocity = velocity.linear_interpolate(direction * speed, accel * delta)
	
	#if player on floor
	if is_on_floor():
		#make him stick  
		y_Velocity = -0.01
	else:
		#make him fall
		y_Velocity = clamp(y_Velocity - gravity, -max_terminal_velocity, max_terminal_velocity)
	#if "jump"(default: space) was pressed	
	if Input.is_action_just_pressed("jump") and is_on_floor():
		#accelearte him in the air
		y_Velocity = jump_power
	#apply jump
	velocity.y = y_Velocity
	velocity = move_and_slide(velocity, Vector3.UP)

I hope the comments don’t make it to unreadable, I added them for my understanding, I guess copying the code into the godot editor works best. Feel free to correct me on the comments since I am still learning and trying to wrap my head around the godot engine and gdscript.

I’m not seeing anything right off that sticks out.

  1. Any errors from the debugger at the bottom when you run the project?
  2. Are all of your inputs mapped correctly in Project -> Project Settings -> Input Map?

I’m doubtful this will help but change the following code and see if that helps, also see if you can see the print statements in the output for all of your different key presses:

func movement_calc(delta):
    #Player direction control 
    var direction = Vector3.ZERO

   #while "move_forward"(default: W) is pressed
    if Input.is_action_pressed("move_forward"):
        #change direction in negative z
        print("forwards")
        direction -= transform.basis.z
    #while "move_backward"(default: S) is pressed
    if Input.is_action_pressed("move_backward"):
        #change direction in positive z
        print("backwards")
        direction += transform.basis.z 
    #while "move_left"(default: A) is pressed
    if Input.is_action_pressed("move_left"):
        #change direction in negative x
        print("left")
        direction -= transform.basis.x
    #while "move_right"(default: D) is pressed
    if Input.is_action_pressed("move_right"):
        #change direction in positive x 
        print("right")
        direction += transform.basis.x

then a little ways down:

    if is_on_floor():
        #make him stick  
        y_Velocity = 0

timothybrentwood | 2021-04-29 23:57

Hey timothybrentwood,

thank you for you reply. I finally had some time to check your answer. I checked the key mapping and even remapped all keys just to be sure, they are all correct. I tried your code changes but it still doesn’t work correctly. I can only walk backwards :frowning:

Your idea to print the pressed keys at least go me the hint that for some reason, godot only registered the backward key. I have no idea why tho.

The keys are mapped correctly and the code is without typing errors. Do you maybe have an idea what goes wrong?

Crow | 2021-05-01 08:55

Huh that is strange. If you are not seeing the print statements it means that the engine either doesn’t think you’re pressing the key down or that key press is being consumed by another part of your game before it gets to your KinematicBody script - maybe there’s another scenario but I can’t think of it off hand. Here are some things I would try:

  1. Remap moving, say left and right, to your mouse button left and mouse button right - if you are using your mouse else where unbind those input maps. See you can at least see the print statements in the output.
  2. Remap all moving directions to the F1, F2, F3, F4 keys. See if you can at least see the print statements.

If 2 works it’s likely that the key presses are getting consumed elsewhere before you get to the KinematicBody script. If 1 works but NOT 2 then something weird is going on with your keyboard - like you have a German keyboard and English OS and like your Y key is registering the key code of the Z key with Godot (same key positions on German vs English keyboards) so when you press Y Godot thinks your pressing Z. I HIGHLY doubt that is the case but things can slip through the cracks. I would think Godot would handle this automatically, but the OS object does expose methods related to it: keyboard_get_current_layout() keyboard_get_layout_count() keyboard_get_layout_language(index: int) keyboard_get_layout_name(index: int) keyboard_set_current_layout(index: int)

timothybrentwood | 2021-05-01 09:35

I have a solution finally. I tried out remapping again, found out that apparently Godot has a problem with the names: "mForward" works "move_forward" doesn’t. I don’t know if that is a bug or just has to do with something Godot internal but it works now.

Thank you for helping out. I hope you have a great day :slight_smile:

Crow | 2021-05-01 15:02