Attention | Topic was automatically imported from the old Question2Answer platform. | |
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