0 votes

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)
Godot version 3.3.2.stable
in Engine by (14 points)

1 Answer

+1 vote

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.

by (20,677 points)

Thanks :) 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.

Thanks :) 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.

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. :) Got everything working now, again, thanks a ton.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.