How can I make this code cleaner, so the animations are in their own function?

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

Hi there I’m new to godot, and I am currently making a platformer game. I have the character movement set up and everything works, but I was wondering if there was a way to make the code cleaner so the the animations are in a separate function to the movement. Also if there are any other improvements to the code would be greatly appreciated.

Here is my code:

extends KinematicBody2D

const UP = Vector2(0,-1)
const GRAVITY = 20
const ACCELERATION = 50
const MAX_SPEED = 200
const JUMP_HEIGHT = -550

var motion = Vector2()

func _physics_process(_delta):
motion.y += GRAVITY
var friction = false

if Input. is_action_pressed("right"):
	motion.x = min(motion.x+ACCELERATION, MAX_SPEED)
	$Sprite.flip_h = false
	$Sprite.play("walk")
	
elif Input. is_action_pressed("run_right"):
	motion.x = min(motion.x+ACCELERATION, MAX_SPEED)
	$Sprite.flip_h = false
	$Sprite.play("run")
	
elif Input. is_action_pressed("left"):
	motion.x = max(motion.x-ACCELERATION, -MAX_SPEED)
	$Sprite.flip_h = true
	$Sprite.play("walk")

elif Input. is_action_pressed("run_left"):
	motion.x = max(motion.x-ACCELERATION, -MAX_SPEED)
	$Sprite.flip_h = true
	$Sprite.play("run")
	
else:
	$Sprite.play("idle")
	friction = true
	motion.x = lerp(motion.x, 0, 0.2)
		
if is_on_floor():
	if Input. is_action_just_pressed("jump"):
		motion.y = JUMP_HEIGHT
	if friction == true:
		motion.x = lerp(motion.x, 0, 0.2)
else:
	if motion.y < 0:
		$Sprite.play("jump")
	else:
		$Sprite.play("fall")
	if friction == true:
		motion.x = lerp(motion.x, 0, 0.05)
	
motion = move_and_slide(motion, UP)
pass
:bust_in_silhouette: Reply From: BrunoFreezee

I’m guessing you’ve watched HeartBeast, it’s a great channel, I’ve learned a lot and still learning there, so heres is some improvements you can do, you may copy the code but please, study it, undertand how it works

some tips:
*setting up a direction system is great to do movement with less code, like this I’ve used get_action_strength to give 1 and -1 for the buttons pressed, then multiplying for the speed

*sprite.flip_h = motion.x < 0 avoid checking for the side your looking every time

*you set up a run_right and left button, but never changes the speed, I don’t see how it works, maybe you were still working on it

*I think is better to saparate every thing in variables, like: friction, air_friction, onready var sprite = $Sprite, maybe a wall_friction, a input_run…

Good Luck! :slight_smile:

extends KinematicBody2D

var motion = Vector2.ZERO

const gravity = 1200
const walk_speed = 250
const run_speed = 500
const acceleration = 50
const jump_force = -550

func _physics_process(delta):
	motion.y += gravity * delta
	
	var input_direction = Input.get_action_strength("ui_right") - Input.get_action_strength("ui_left")
	var input_run = Input.is_action_pressed("ui_run")
	$Sprite.flip_h = motion.x < 0
	
	if input_direction:
		motion.x += input_direction * acceleration
		if !input_run:
			motion.x = clamp(motion.x, -walk_speed, walk_speed)
		else:
			motion.x = clamp(motion.x, -run_speed, run_speed)
			
	if is_on_floor():
		if !input_direction:
			motion.x = lerp(motion.x, 0, 0.2)
			
		if Input.is_action_just_pressed("ui_up"):
			motion.y = jump_force
	else:
		if !input_direction:
			motion.x = lerp(motion.x, 0, 0.05)
			
	motion = move_and_slide(motion, Vector2.UP)