How do I move an object with the parent, but keep it's position and rotation relative to the parent when the parent rotates?

I have an enemy with a health bar. I need the enemy to rotate without causing the health bar to rotate around itself. What I mean by this, is the health bar is situated below the enemy, and when it rotates, it currently orbits the parent. I need it not to do this, but remain stationary. I also need to have the health bar parented to the enemy so that a script will work. How do I do TryGetComponentInParent? - Questions & Answers - Unity Discussions

I figured out a solution! transform.parent.Find("Sibling").gameObject.GetComponent<Script>() to get the variable from the other script, then I used a position constraint to keep the health bar under the enemy. Thanks for all you’re help!

To move an object with its parent while maintaining its position and rotation relative to the parent, you can use the Transform.SetParent method and specify the worldPositionStays parameter as false. This will keep the object’s local position and rotation relative to the parent, while still allowing it to move with the parent.

Here is an example:

Assume that “healthBar” is the object that you want to move with the parent while maintaining its local position and rotation.

    // Set the parent of the health bar to the enemy object, while maintaining its local position and rotation.
    healthBar.transform.SetParent(enemy.transform, false);

This will keep the health bar stationary relative to the enemy, even when the enemy rotates.