What could cause homing missiles to have different behavior in different scenes?

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

I’m using a slightly modified version of the KidsCanCode homing missile recipe. I got it working fine on a test scene I made, but on the main scene when i try to have an enemy spawn it, instead of following the players like they’re supposed to the missiles immediately swerve to the right upon detecting a target.

Test scene spawn code:
extends Node2D
var MISSLE = preload("res://enemies/boss_attacks/homing_bullet.tscn")
func _ready():
    Global.create_timer(2, self, "spawn_bullets", false)
func spawn_bullets():
   var missle = MISSLE.instance()
   add_child(missle)
   missle.start(Transform2D(Vector2(0,1),Vector2(0,1),$bullet_spawn.position))

enemy in main scene missile spawn code:
extends "res://enemies/boss_template.gd"
var MISSLE = preload("res://enemies/boss_attacks/homing_bullet.tscn")
func _ready():
Global.create_timer(3, self, "homing_missle_firing", false)
func homing_missle_firing():
print("missle spawning")
var missle = MISSLE.instance()
get_parent().add_child(missle)
missle.start(Transform2D(Vector2(0,1),Vector2(0,1),$missile_spawns/top_left.global_position))
print(missle.global_position)

Clip showing the difference between test and main scene

:bust_in_silhouette: Reply From: TheJollyReaper

After taking a break and looking at this with fresh eyes, I noticed that in my game scene the missiles were actually being made children of the enemy node, not the main node like I believed. Correcting that fixed the missile behavior.

Today I learned get_parent() does not get the node at the very top of the tree, at least when called within an instanced object.

Taking a break can do miracles, when you are stuck :slight_smile:

whiteshampoo | 2020-06-24 06:58