projectile problems

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By cobracon

in my game I run this code on the player

func _physics_process(delta: float) → void:
if Input.is_action_just_pressed(“attack”):
var magic = spell.instance()
get_parent().add_child(magic)

var spell = load(“res://scenes/progectile.tscn”)

and this on my projectile

extends KinematicBody2D
var velocity = Vector2.ZERO
func _ready() → void:
if Global.area == 1:
velocity.y = -190
if Global.area == 2:
velocity.y = 190
if Global.area == 3:
velocity.x = 190
if Global.area == 4:
velocity.x = -190

func _physics_process(delta: float) → void:
move_and_slide(velocity)

func _on_Timer_timeout() → void:
queue_free()

but whenever my player moves so does my projectile
how do I fix it

:bust_in_silhouette: Reply From: Cronos87

Hello,

Try to attach your spell’s instance to the root of your scene instead of the parent.

I hope this help.

ummm how do I do that

cobracon | 2021-08-29 20:01

Try this: get_tree().get_current_scene().add_child(spell_instance)

Cronos87 | 2021-08-29 21:17