![]() |
Attention | Topic was automatically imported from the old Question2Answer platform. |
![]() |
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)