Use listener in a 3D project

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

Hi,
How I can make that a KinematicBody hearts for a sound and when this occurs, it go to the sound location?
Thanks in advanced.

:bust_in_silhouette: Reply From: Wakatta

Most times in game development you have to give your player the illusion that something is happening not actually do what is visually or in this case audibly represented.

Not that what you want to do isn’t possible, it just has easier ways to implement it.
For example each Node that plays a sound can emit a signal broadcasting it’s location
And have all interested KinematicBody connect and move accordingly when the signal is emitted

An example.

func _ready():
    for node in get_tree().get_current_scene().get_children():
        if node is $AudioStreamPlayer3D:
            node.connect("finished", self, "_follow_sound", [node])

func _follow_sound(node):
    move_toward(node.global_trasform.origin)

Wakatta | 2021-04-08 01:17