This site is currently in read-only mode during migration to a new platform.
You cannot post questions, answers or comments, as they would be lost during the migration otherwise.
0 votes

How do I make my character stop moving after i stop pressing the button?
my code is below:

extends KinematicBody2D

const UP = Vector2(0, -1)
const SPEED = 125
const MAX_SPEED = 125
const GRAVITY = 20
const FRICTION = 0.1
const JUMP_HEIGHT = 500
const MAX_JUMP_HEIGHT = 1000
const JUMPS = 3

var motion = Vector2()
var jumps_available := JUMPS

func _physics_process(delta):

    motion.y += GRAVITY

    if Input.is_action_pressed("right"):
        motion.x += SPEED
        motion.x = min(motion.x+SPEED, MAX_SPEED)
        motion.x = lerp(motion.x, 0, FRICTION)
    elif Input.is_action_pressed("left"):
        motion.x -= SPEED
        motion.x = max(motion.x-SPEED, -MAX_SPEED)
        motion.x = lerp(motion.x, 0, FRICTION)

        # _physics_process
    if Input.is_action_just_pressed("jump") and jumps_available > 0:
        motion.y -= JUMP_HEIGHT
        motion.y = max(motion.x-JUMP_HEIGHT, -MAX_JUMP_HEIGHT)
        jumps_available -= 1

# _physics_process
    if is_on_floor():
        jumps_available = 2
    motion = move_and_slide(motion, UP)
Godot version v3.4.2
in Engine by (37 points)

1 Answer

+1 vote
Best answer
       motion.x = 0
by (755 points)
selected by

thankyou so much :)

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.