Why does my character fall slower with no action pressed?

Godot 4

So when I press and hold the jump action or any action afterwards character falls fast, but I just press jump and let go he slowly falls to ground. Secondary question is why does my full animation not play when jumping/attacking?
Here’s the code so far…

extends CharacterBody2D

const speed = 300
const max_speed = 400
const jump = -500
const friction = 500
const gravity = 50
var input = Vector2.ZERO
var health = 100

func _ready():
print(“ready”)
pass

func _physics_process(delta):
input = get_input()
position += velocity * delta

velocity.y += gravity 
if input == Vector2.ZERO:
	if velocity.length() > (friction * delta):
		velocity = velocity.normalized() * (friction * delta)
		#print("got input")
	else:
		velocity = Vector2()
else:
	velocity += (input * speed * delta)
	velocity = velocity.limit_length(max_speed)
if Input.is_action_pressed("slash"):
	$animation.play("attack")
	velocity.x = 0
	
elif  Input.is_action_pressed("jump") && is_on_floor():
	velocity.y = jump
elif velocity.y < 0:
		$animation.play("jump")
		
elif Input.is_action_pressed("left"):
	$animation.play("walking")
	$animation.flip_h = true
	#print("flipped")
elif Input.is_action_pressed("right"):
	$animation.play("walking")
	$animation.flip_h = false
	#print("walking")
else:
	$animation.play("still")
if Input.is_action_pressed("duck") and velocity.length() == 0:
	$animation.play("duck")
	#print("ducking")
elif Input.is_action_pressed("duck") and velocity.length() > 0:
	$animation.play("slide")
	velocity *= .95
	#print("sliding")
	if velocity.length() < 100:
		velocity.x = 0
		$animation.play("duck")
		

move_and_slide()

func get_input():
input.x = int(Input.is_action_pressed(“right”)) - int(Input.is_action_pressed(“left”))
input.y = int(Input.is_action_pressed(“jump”))
return input.normalized()

Becasue you’re changing the entire vector here that also affects the vertical speed, you need to only affect the x component.

Should I not be using the “if input == Vector2():” line like that? every time I use velocity.x its sends an error for using Vector2 as float.

Because you need to compare only the x component, you need to compare using a float value like:

if input.x == 0.0:

You also need to adapt all the cited code to only work with the x component instead the entire vector

Thank you!! that worked like a charm! Now what can I do to stop this physics_process from cutting my jump and attack animation short? If you don’t mind pointing me in the right direction.

This looks like the case you need to lock the animation for not start other animations until the selected one finishes, you can do something like that:

# Create a variable to store if anim is locked
var is_anim_locked := false

func play_animation_with_lock(p_anim_name: String, p_lock: bool) -> void:
	# Same animation, do nothing
	if $animation.current_animation == p_anim_name:
		return
	
	# Is doing some animation you don't want to cancel
	if is_anim_locked:
		return
	
	is_anim_locked = p_lock
	$animation.play(p_anim_name)

	# If lock is true, only unlock after the animation ends
	if p_lock:
		await $animation.finished
		is_anim_locked = false

# So you can call for animations what you want to lock like the attack or the jump:
play_animation_with_lock("attack", true)

# And for others like moving/idle you call
play_animation_with_lock("walking", false)
1 Like

Okay so it seems the “current_animation” property is for the Animation Player. I should have specified that I was using Animated Sprite 2D, is there an equivalent to this property for the animated sprites? if not I may just have to switch over the the Animation Player.

Replace:

# Same animation, do nothing
if $animation.current_animation == p_anim_name:
	return

For:

# Same animation, do nothing
if $animation.animation == p_anim_name:
	return

And this:

if p_lock:
	await $animation.finished
	is_anim_locked = false

For:

if p_lock:
	await $animation.animation_finished
	is_anim_locked = false
1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.