Keyboard inputs
Asked Answered
C

8

0

i want to start a whole animation but with a single press and i don't want that for seeing the whole animation i need to press constantly the button
in this case i used Input.is_key_label_pressed(KEY_E):
but my question is : there is another method that an be used to start the animation with a single press?

i am new to godot so thanks for the help 🙂

Carrara answered 16/11, 2023 at 17:26 Comment(0)
P
0

Carrara You could maybe use a shortcut of a Control but in general it is probably the easiest to just check the keyboard input events if a certain key or action is pressed and then start the animation if it isn’t playing already.

Plain answered 16/11, 2023 at 17:41 Comment(0)
C
0

Plain this is the code that i'm using:
extends CharacterBody2D

var health = 10
const SPEED = 300.0
const JUMP_VELOCITY = -400.0

var gravity = ProjectSettings.get_setting("physics/2d/default_gravity")
@onready var anim = get_node("AnimationPlayer")

func _physics_process(delta):

if not is_on_floor():
	velocity.y += gravity * delta

# Handle Jump.
if Input.is_action_just_pressed("ui_accept") and is_on_floor():
	velocity.y = JUMP_VELOCITY
	anim.play("Jump")

# Get the input direction and handle the movement/deceleration.
# As good practice, you should replace UI actions with custom gameplay actions.
var direction = Input.get_axis("left", "right")
if direction == -1:
	get_node("AnimatedSprite2D").flip_h = true
elif direction == 1:
	get_node("AnimatedSprite2D").flip_h = false

if direction:
	velocity.x = direction * SPEED
	if velocity.y == 0 and Input.is_key_label_pressed(KEY_E):
		velocity.x = 0
		anim.play("Sword_Attack")
	elif velocity.y == 0:
		anim.play("Run")
		
else:
	velocity.x = move_toward(velocity.x, 0, SPEED)
	if velocity.y == 0 and Input.is_key_label_pressed(KEY_E):
		anim.play("Sword_Attack")
	elif velocity.y == 0:
		anim.play("Idle")
move_and_slide()

i was wondering if there is another method of input where i don't need to hold down the button for seeing the whole animation, how can i change the code to make this happen ?

Carrara answered 16/11, 2023 at 17:49 Comment(0)
P
0

Carrara Wait, which animation are we talking about and what do mean by you have to hold down the button to see the whole animation?

Plain answered 16/11, 2023 at 18:1 Comment(0)
C
0

Plain the sword attack and i mean that if there is a way that i can play the single animation in a single tap on the key instead than holding my key down

if i try to hit with a single tap now, the animation starts but it plays only the first frame and then it returns to the idle animation

Carrara answered 16/11, 2023 at 18:5 Comment(0)
P
0

Carrara With Input.is_key_label_pressed(KEY_E) you check if the E key is currently pressed down which would always be true as long as you hold the key down so you always restart your animation from the beginning.

Try declaring something like an "attack" action and use Input.is_action_just_pressed(), like you did above for the jump. That would only return true once on the first frame when you press your attack key.

Plain answered 16/11, 2023 at 18:35 Comment(0)
C
0

Plain
https://clipchamp.com/watch/regDDebn0iQ
if i do this when i hit the e this happens

Carrara answered 16/11, 2023 at 18:55 Comment(0)
G
0

OH you want the entire animation to play all the way through when you press the key, and the problem is that when you're not holding the key, it goes back to Run or Idle. There are two ways to solve this:

  1. Learn how to use AnimationNodeStateMachine which will allow you to have precise control over when animations are allowed to transition. This seems like a decent tutorial for it:

  2. Use your own code to ensure that other animations don't play while the Sword_Attack animation is playing. You also may want to change movement behavior while the attack is playing. That is up to you. You can do this by checking
    if not (anim.current_animation == "sword_attack" and anim.is_playing()): or by waiting for the signals on the AnimationPlayer such as anim.animation_finished

Geezer answered 16/11, 2023 at 20:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.