Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | Gamermatteo168 |
my spell keeps deleting itself for some reason. this is my first godot game. in the game there is 2 players whoa re wizards and they can each shoot a spell, the first one to cast a spell at the other wizard wins. there are some walls mixed in the game scene, they just consist of a sprite,staticbody2d and a colisonpolygon2d. im trying to make it so that when the spell casts it dies when it hits the colider, but instead i think its dying beside the player. the worst part is that they still dont even self destruct when they hit the wall! im not sure how to fix this, and i cant really find anything that can help.
spell script:
extends Area2D
const speed =150
var velocity =Vector2()
onready var AnimationPlayer = $AnimationPlayer
var direction = 1
func set_spell_direction(dir):
direction = dir
if dir == -1:
$Sprite.flip_h = true
func _physics_process(delta):
velocity.x = speed * delta * direction
translate(velocity)
AnimationPlayer.play("Spell")
func _on_VisibilityNotifier2D_screen_exited():
queue_free()
func _on_Spell_area_entered(area):
queue_free()
my players script:
extends KinematicBody2D
const Acceleration = 500
const Max_speed = 150
const Friction = 400
const Spell = preload("res://Scenes/Spell.tscn")
var velocity = Vector2.ZERO
var can_cast = true
onready var AnimationPlayer = $AnimationPlayer
onready var spell_item_position = $Spell_Cast_Posistion
func _physics_process(delta):
var input_vector = Vector2.ZERO
input_vector.x = Input.get_action_strength("ui_right") - Input.get_action_strength("ui_left")
input_vector.y = Input.get_action_strength("ui_down") - Input.get_action_strength("ui_up")
input_vector = input_vector.normalized()
if input_vector != Vector2.ZERO:
if input_vector.x > 0:
AnimationPlayer.play("Walk")
$Sprite.flip_h = false
else:
AnimationPlayer.play("Walk")
$Sprite.flip_h = true
velocity = velocity.move_toward(input_vector * Max_speed, Acceleration * delta)
else:
AnimationPlayer.play("Idle")
velocity = velocity.move_toward(Vector2.ZERO, Friction * delta)
velocity = move_and_slide(velocity)
if Input.is_action_pressed("ui_shoot"):
Cast()
AnimationPlayer.play("Cast_Spell")
func Cast():
var SPELL = Spell.instance()
get_parent().add_child(SPELL)
SPELL.position = get_node("Spell_Cast_Posistion").global_position
func _on_Hitbox_area_entered(area):
queue_free()