Godot Version
Question
Hi! I’m new to Godot, so I don’t know the language very well. I made a character class for a 2D platformer. It can jump, move and is animated.
I followed this tutorial for the shooting.
My problem is that, whenever I press the shooting key (F), my character completely stops. I can’t move it, enemies don’t detect it, it’s animations stops too. I noticed this happens when “add_child(arrow_instance)” runs. I know cause I deleted that line of code and the character works. What is happening? Why is creating the child node doing this? I’ll leave my character class script here:
extends CharacterBody2D
const SPEED = 130.0
const JUMP_VELOCITY = -300.0
var baston_equipado = true
var arrow = preload(“res://Scenes/arrow.tscn”)
Get the gravity from the project settings to be synced with RigidBody nodes.
var gravity = ProjectSettings.get_setting(“physics/2d/default_gravity”)
#Llamadas
@onready var animated_sprite = $AnimatedSprite2D
@onready var timer = $Timer
func _physics_process(delta):
# Add the gravity.
if not is_on_floor():
velocity.y += gravity * delta
# Handle jump.
if Input.is_action_just_pressed("jump") and is_on_floor():
velocity.y = JUMP_VELOCITY
# Get the input direction -1, 0 ó 1
var direction = Input.get_axis("move_left", "move_right")
#Cambia el sprite de lado
if direction > 0:
animated_sprite.flip_h = false
elif direction < 0:
animated_sprite.flip_h = true
#Animaciones
if is_on_floor():
if direction == 0:
animated_sprite.play("Idle")
else:
animated_sprite.play("run")
else:
animated_sprite.play("jump")
if direction:
velocity.x = direction * SPEED
else:
velocity.x = move_toward(velocity.x, 0, SPEED)
move_and_slide()
var mouse_pos = get_global_mouse_position()
$Marker2D.look_at(mouse_pos)
#Llama a Disparar
if Input.is_action_just_pressed("Shoot") && timer.is_stopped() && baston_equipado:
print("Tiro")
shoot()
var arrow_instance = arrow.instantiate()
arrow_instance.rotation = $Marker2D.rotation
arrow_instance.global_position = $Marker2D.global_position
add_child(arrow_instance)
#Disparar
func shoot():
timer.start()
func _on_timer_timeout():
timer.stop()
Thank you for reading. This is my first post, I am sorry if I didn’t do things in the proper order.