Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | WaterDev |
So I’m new to this program and so far it’s been really easy and fun to use it.
But I’ve encountered one problem. My character won’t stop accelerating no matter the code i type to tell the thing to stop accelerating when it goes MAX_SPEED.
Here’s my code so far
extends KinematicBody2D
const UP = Vector2(0, -1)
const GRAVITY = 20
const ACCELERATION = -10
const MAX_SPEED = 200
const JUMP_HEIGHT = -600
var motion = Vector2()
func _physics_process(delta):
var friction = false
motion.y += GRAVITY
if Input.is_action_pressed("ui_left"):
$Sprite.flip_h = true
$Sprite.play("Run")
motion.x = min(motion.x+ACCELERATION, MAX_SPEED)
elif Input.is_action_pressed("ui_right"):
$Sprite.flip_h = false
$Sprite.play("Run")
motion.x = max(motion.x-ACCELERATION, -MAX_SPEED)
else:
$Sprite.play("Idle")
friction = true
if is_on_floor():
if friction == true:
motion.x = lerp(motion.x, 0, 0.2)
if Input.is_action_just_pressed("ui_up"):
$Sprite.play("Jump")
motion.y = JUMP_HEIGHT
else:
$Sprite.play("Jump")
motion.x = lerp(motion.x, 0, 0.05)
motion = move_and_slide(motion, UP)
pass