0 votes

All of my "is pressed" codes work just fine but none of the "is just pressed" codes work. What am i doing wrong? I'm making a 2D platformer
Heres my player code:

extends KinematicBody2D

const UP = Vector2(0, -1)
const GRAVITY = 15
const ACCELERATION = 23
const MAXSPEED = 125
const JUMP
HEIGHT = -300
var collisioninfo = moveand_collide(Vector2(0,0))
var motion = Vector2()

func physicsprocess(delta):
motion.y += GRAVITY
var friction = false

if Input.is_action_pressed("ui_right"):
    motion.x = min(motion.x+ACCELERATION, MAX_SPEED)
    $Sprite.flip_h = false
    $AnimationPlayer.play("Run")
elif Input.is_action_pressed("ui_left"):
    $Sprite.flip_h = true
    $AnimationPlayer.play("Run")
    motion.x = max(motion.x-ACCELERATION, -MAX_SPEED)
else: 
    $AnimationPlayer.play("Idle")
    friction = true




if is_on_floor():
    if Input.is_action_just_pressed("ui_up"):
        motion.y = JUMP_HEIGHT
    if friction == true: 
        motion.x = lerp(motion.x, 0, 0.18)
else:
    if motion.y < 0:
        $AnimationPlayer.play("Jump")

if friction == true: 
    motion.x = lerp(motion.x, 0, 0.05)

if Input.is_action_just_pressed("ui_attack"):
    $AnimationPlayer.is_playing("Attack")


motion = move_and_slide(motion, UP)
asked Jun 11 in Engine by prodtmk (12 points)

1 Answer

0 votes

It may already played the animation and ended, so you have to loop the animation.

Also, it is better to put the play animation inside the process or _physicsprocess and have a variable checker there that is changed depending on the user's input.

Example:

var isJumping= false

func _ready():
    $AnimationPlayer.get_animation("Jump").set_loop(true)

func _input(delta):
    if Input.is_action_pressed("ui_up"):
        isJumping= true

    if Input.is_action_just_pressed("ui_up"):
        isJumping= false

func _process(delta):
    if isJumping:
         $AnimationPlayer.play("Jump")
answered Jun 11 by nightrobin (22 points)
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 How to use this Q&A? before posting your first questions.