Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

need help, how to stop my health bar from rotating

Discussion in '2D' started by Zexin21, Nov 18, 2021.

  1. Zexin21

    Zexin21

    Joined:
    Sep 16, 2021
    Posts:
    13
    I have a Health bar that is a child to my player, but I don't know how to keep it from rotating and still follow the player.
     
  2. thetrapman

    thetrapman

    Joined:
    Mar 9, 2019
    Posts:
    6
  3. Zexin21

    Zexin21

    Joined:
    Sep 16, 2021
    Posts:
    13
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,780
    Here are two possible approaches:

    Approach 1: have the health bar be a separate GameObject (not a child) that has a script to stay with the player, perhaps adding an offset, and doing so perhaps in LateUpdate() so you know the player has moved

    Approach 2: (more intrusive): restructure your player so the root object does NOT rotate, but only a child object rotates, and then another non-rotating child object would be the health bar, offset from the root. You would only move the root object, but you would only rotate the child visuals.

    EDIT: I made a gist of option #1 above because this question comes up so much. Gist located here:

    https://gist.github.com/kurtdekker/1152ceb81a7de7420b796bcca5a86cb9

    Full unitypackage demo scene attached herein.
     

    Attached Files:

    Last edited: Nov 18, 2021
  5. Chris-Trueman

    Chris-Trueman

    Joined:
    Oct 10, 2014
    Posts:
    1,256
    Approach 3: Get a reference to the health bar's transform and set the rotation to identity. If the health bar script is on the health bar, all you need to do is add the line transform.rotation = Quaternion.identity; in LateUpdate().
     
    Kurt-Dekker likes this.
  6. Zexin21

    Zexin21

    Joined:
    Sep 16, 2021
    Posts:
    13
    how would I set the rotation to identity?
     
  7. Zexin21

    Zexin21

    Joined:
    Sep 16, 2021
    Posts:
    13
    whenever I do this, my healthbar still rotates, but it stays under the character
    Edit: I forgot to have the healthbar not be a child
     
    Last edited: Nov 23, 2021
  8. Chris-Trueman

    Chris-Trueman

    Joined:
    Oct 10, 2014
    Posts:
    1,256
    Code (CSharp):
    1. transform.rotation = Quaternion.identity;