I altered 2D movement script to make it nicer looking using functions, now my player cannot move, only turn

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

Basically I used a tutorial to begin my movement script, and then decided to go more in depth with it. By the time I added all the functionality I wanted the script was slow, a little buggy, and just a mess. So instead I redid the whole thing using my old script as a guide. I turned most functionality into specific functions and now I can’t do anything except have my sprite turn left and right. Any help would be appreciated.

	extends KinematicBody2D
#movement script
const UP=Vector2(0,-1)
var motion=Vector2()
var walk=32
var run=48
var dash=100
var gravity=3
var jump_force=-70
var acceleration=1
var friction_floor=.4
var friction_air=.2
var friction_wall=.5
var jump_counter=2
var gravity_multiplier=1
var dash_barrier=50
var dash_decel=3
onready var sprite = get_node("Player")
var max_jump=2
var dash_timer=3
var timer=0

var health=20
var mana=20

func _physics_process(delta):
	motion.y+=gravity*gravity_multiplier
	timer+=delta
	
	if Input.is_action_just_pressed("control_sprint"):
		motion=dash_func(motion)
	if Input.is_action_pressed("control_sprint"):
		motion=run_func(motion)
	if Input.is_action_just_pressed("control_up"):
		motion=jump(motion)
	if Input.is_action_pressed("control_right") and not is_on_wall():
		motion=right(motion)
	if Input.is_action_pressed("control_left") and not is_on_wall():
		motion=left(motion)
	if not is_on_floor():
		if is_on_wall():
			if Input.is_action_pressed("control_right"):
				motion=wall_jump_right(motion)
			elif Input.is_action_pressed("control_left"):
				motion=wall_jump_left(motion)
	if motion.y>420:
		get_tree().reload_current_scene()
	jump_check()
	friction()
	direction()
	
func dash_func(motion):
	if Input.is_action_pressed("control_right"):
		if timer>dash_timer:
			motion.x=dash
			sprite.play("dash_right")
			timer=0
	if Input.is_action_pressed("control_right"):
		if timer>dash_timer:
			motion.x=-dash
			sprite.play("dash_left")
			timer=0
	return move_and_slide(motion, UP)
func run_func(motion):
	if is_on_floor():
		if Input.is_action_pressed("control_right"):
			if motion.x<run:
				motion.x+=acceleration
				if motion.x<30:
					motion.x+=1.75*acceleration
				sprite.play("run_right")
		if Input.is_action_pressed("control_left"):
			if motion.x>-run:
				motion.x+=-acceleration
				if motion.x>-30:
					motion.x+=-1.75*acceleration
				sprite.play("run_left")
	else:
		if Input.is_action_pressed("control_right"):
			if motion.x<run:
				motion.x+=acceleration/2.5
		if Input.is_action_pressed("control_left"):
			if motion.x>-run:
				motion.x+=-acceleration/2.5
	return move_and_slide(motion, UP)
func jump(motion):
	jump_counter-=1
	if not is_on_wall() and is_on_floor():
		if Input.is_action_perssed("control_right"):
			sprite.play("jump_start_right")
			motion.y=jump_force
		elif Input.is_action_pressed("control_left"):
			sprite.play("jump_start_left")
			motion.y=jump_force
		else:
			motion.y=jump_force
	return move_and_slide(motion, UP)
		
func right(motion):
	if is_on_floor():
		sprite.play("walk_right")
		if motion.x<0:
			motion.x+=1.5*acceleration
		if motion.x<walk:
			motion.x+=1.2*acceleration
		if motion.x<20:
			motion.x+=2.2*acceleration
	else:
		sprite.play("idle_right")
		if motion.x<0:
			motion.x+=acceleration
		if motion.x<walk/2.5:
			motion.x+=acceleration
	return move_and_slide(motion, UP)
			
func left(motion):
	if is_on_floor():
		sprite.play("walk_left")
		if motion.x>0:
			motion.x+=-1.5*acceleration
		if motion.x>-walk:
			motion.x+=-1.2*acceleration
		if motion.x>-20:
			motion.x+=-2.2*acceleration
	else:
		sprite.play("idle_left")
		if motion.x>0:
			motion.x+=-acceleration
		if motion.x>-walk/2.5:
			motion.x+=-acceleration
	return move_and_slide(motion, UP)
						
func jump_check():
	if is_on_floor():
		jump_counter=max_jump
	elif is_on_wall():
		jump_counter=max_jump-1
	motion=move_and_slide(motion, UP)
		
func friction():
	if is_on_floor():
		if motion.x>0:
			motion.x+=-friction_floor*acceleration
		elif motion.x>80:
			motion.x+=-friction_floor*3*acceleration
		if motion.x<0:
			motion.x+=friction_floor*acceleration
		elif motion.x<-80:
			motion.x+=friction_floor*3*acceleration
	elif is_on_wall():
		if motion.y>0:
			motion.y+=.5*gravity
	motion=move_and_slide(motion, UP)
			
func direction():
	if Input.is_action_just_released("control_right"):
		sprite.play("idle_right")
	if Input.is_action_just_released("control_left"):
		sprite.play("idle_left")

func wall_jump_right(motion):
	if not Input.is_action_just_pressed("control_jump"):
		if motion.x<walk/2.5:
			motion.x+=acceleration
	else:
		motion.x=-dash/1.5
		motion.y=jump_force
	return move_and_slide(motion, UP)
func wall_jump_left(motion):
	if not Input.is_action_just_pressed("control_jump"):
		if motion.x>-walk/2.5:
			motion.x+=-acceleration
	else:
		motion.x=dash/1.5
		motion.y=jump_force
	return move_and_slide(motion, UP)
	

I can’t do anything except have my sprite turn left and right

Cannot reproduce. I just run your code and everything worked: moving left, moving right, jumping, dashing, running. The only lines I removed where the ones related to the AnimatedSprite and that shouldn’t change anything…

Maybe you can upload an example project?

njamster | 2020-04-17 09:40