Kinematicbody2d won't move on the x axis for some reason.

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

I am not sure why my movement code is not moving my player’s kinematic body on the x axis. Jump works fine, animations change on input…just, no x movement.
Pretty new to all of this, but it looks like it should work?

 extends KinematicBody2D
#Variables and Constants
const MAX_JUMP_COUNT = 2
const FLOOR = Vector2(0,-1)
const gravity = 3000

var sprite_node
var velocity = Vector2.ZERO
var state_machine
var jump_count = 0
var jump_speed = 750

var attack_cooldown_time = 1000
var next_attack_time = 0
var attack_damage = 1
var attack_range = 40

var bullet = preload("res://bullet.tscn")

var speed = 450
export (float, 0, 1.0) var friction = 0.1
export (float, 0, 1.0) var acceleration = 0.50



func jump():
	velocity.y = -jump_speed
	
func _ready():
	set_process(true)
	set_process_input(true)
	state_machine = $AnimationTree.get("parameters/playback")


#ANIMATIONS
func _process (delta):
	if Input.is_action_pressed("left"):
			state_machine.travel("Run")
			$Sprite.flip_h = true
	if Input.is_action_pressed("right"):
			state_machine.travel("Run")
			$Sprite.flip_h = false
	if !is_on_floor() and Input.is_action_pressed("jump"):
			state_machine.travel("Jump")
	if Input.is_action_just_released("left") and is_on_floor():
			state_machine.travel("Idle")
			$Sprite.flip_h = true
	if Input.is_action_just_released("right") and is_on_floor():
			state_machine.travel("Idle")
			$Sprite.flip_h = false
	if velocity.x == 0 and is_on_floor():
			state_machine.travel("Idle")
	
#WALK
func get_input():
	var target_speed = Vector2(speed, velocity.x)
	var dir = 0
	velocity.x = 0
	if Input.is_action_pressed("right") and is_on_floor():
		velocity.x += speed
	if Input.is_action_pressed("left") and is_on_floor():
		velocity.x -= speed
		velocity = lerp(velocity, target_speed, 0.0)
	
#JUMP
func _physics_process(delta):
	var jump_pressed = Input.is_action_just_pressed("jump")

	velocity.y += delta * gravity
	var target_speed = Vector2(speed, velocity.y)

	if is_on_floor():
		jump_count = 0
		
		
	if jump_pressed and jump_count < MAX_JUMP_COUNT:
		jump_count += 1
		jump()
		
		
	#velocity = lerp(velocity, target_speed, 0.0)
	velocity = move_and_slide(velocity, FLOOR)
		
		
func hurt():
	state_machine.travel("hurt")

func die():
	state_machine.travel("die")
	set_physics_process(false)
:bust_in_silhouette: Reply From: kidscancode

You have a get_input() function that sets the x velocity. However, you’re never calling that function.

Also, FYI, putting set_process(true) and set_process_input(true) in _ready() has not been necessary since Godot 2.x.

Thanks :slight_smile: I didn’t know about the updated code as I’m fairly new to Godot and have been devouring every tutorial I can get my hands on.

I actually stumbled across the change you suggested just by process of elimination, so it is working much better now.

HOWEVER, haha, I have another question. Do you have any knowledge as to why my player character would jump much lower in one direction vs the other? When jumping to the left, he reaches full height, but if I switch quickly to running right and jump, he will only go about 1/2 as high. I can’t see why that would happen.

ItsAnti | 2021-06-21 16:19

Thanks :slight_smile: I didn’t know about the updated code as I’m fairly new to Godot and have been devouring every tutorial I can get my hands on.

Well, stay away from things that are more than 3 years old, for sure. Godot 3.0 came out in early 2018.

Do you have any knowledge as to why my player character would jump much lower in one direction vs the other?

Yep, the problem is in your get_input(). You’re lerping the speed, but only when you press “left”, not when you press “right”. You probably don’t want that line indented under the “left” check.

Also, this seems really suspect:

var target_speed = Vector2(speed, velocity.x)

Why would your target speed’s y be the x velocity? I’m pretty sure that should be a .y.

kidscancode | 2021-06-21 17:49

Whoops, yep, that would be wrong.
I’ve got a total of about 2 1/2 days experience with Godot and gdscript, thanks for pointing that out. :slight_smile: Got everything working now, again, thanks a ton.

ItsAnti | 2021-06-21 19:55