![]() |
Attention | Topic was automatically imported from the old Question2Answer platform. |
![]() |
Asked By | demsoyath |
i was trying to add to my player the possibility to attack, while doing that i was left with the animation being cut out right after the first frame by the other animations, after trying to fix it i got another problem, all the animations works as it should when i launch the game, but after using the attack once it get me stuck on the last frame of it and also breaks all the other ones, also lefting me stuck on the last frame of jumping, not running or in idle.
that’s my first project and english is not my main language, i’m sorry if it’s confusing
here’s the code:
extends CharacterBody2D
var health = 10
var is_attacking = false
const SPEED = 300.0
const JUMP_VELOCITY = -400.0
var gravity = ProjectSettings.get_setting("physics/2d/default_gravity")
@onready var anim = get_node("animator")
func _physics_process(delta):
if not is_on_floor():
velocity.y += gravity * delta
if Input.is_action_just_pressed("ui_accept") and is_on_floor():
velocity.y = JUMP_VELOCITY
anim.play("jump")
var direction = Input.get_axis("ui_left", "ui_right")
if direction == -1:
get_node("anim").flip_h = true
elif direction == 1:
get_node("anim").flip_h = false
if direction:
velocity.x = direction * SPEED
if velocity.y == 0 and is_attacking == false:
anim.play("runn")
else:
velocity.x = move_toward(velocity.x, 0, SPEED)
if velocity.y == 0 and is_attacking == false:
anim.play("idle")
move_and_slide()
attack()
func attack():
if Input.is_action_just_pressed("ataque1"):
is_attacking = true
anim.play("attack")
if health <= 0:
queue_free()
Maybe this bit of code has something to do with it:
if velocity.y == 0 and is_attacking == false:
anim.play("idle")
The velocity.y
property is probably a floating-point number. As such, checking for a value of “0” may not be the way to go.
Ertain | 2023-06-19 22:09