How do I make a node health bar stay unrotated?

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

I have a scene with the following hierarchy:

+ PathFollow2D
    + Node2D
        + Sprite

What happens when I use this is both the Node2D (where I draw a health bar) and the Sprite rotate when the PathFollow2D follows the path of its parent Path.

How can I prevent the Node2D from rotating, while allowing the Sprite to rotate - that way the Sprite follows the path, but the health bar floats above the sprite without rotating?

:bust_in_silhouette: Reply From: SIsilicon

Well, you can start off by calling set_as_top level(true) in the Node2D node. This will pretty much disconnect the nodes transformation from the parent node. In this case, the PathFollow2D node. So now we just call this every frame global_position = get_parent().global_position. And along with that, call this $Sprite.global_rotation = get_parent().global_rotation.

Note: set_as_toplevel only needs to be called once.

SIsilicon | 2018-06-04 19:33

:bust_in_silhouette: Reply From: jarlowrey

In addition to what @Sislllcon describes, you could also make a rotation helper function to automatically un-rotate the bar

def rotate(val):
  self.rotation = val
  $Sprite.rotation = 0

I know it might be a mistake but…

You’re a python kind of person aren’t you?

SIsilicon | 2018-06-04 22:12

:bust_in_silhouette: Reply From: lvknd

Simple, no code approach: Draw health bar over enemies - Archive - Godot Forum