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. Have a look at our Games Focus blog post series which will show what Unity is doing for all game developers – now, next year, and in the future.
    Dismiss Notice

Question Ennemy health bar look at the player ?

Discussion in 'Getting Started' started by NoxiFR, Nov 24, 2022 at 5:59 PM.

  1. NoxiFR

    NoxiFR

    Joined:
    Oct 19, 2022
    Posts:
    2
    Hi,
    So I made health bar for ennemy, the health bar is a canvas prefab, which is place like children of my ennemys prefabs.
    I want to make theses health bar "look at" the GameObject "player" (I know there is this fonction), the issue here is:

    When i put the script on the canvas, I can't selelct the "player" Gameobject because its not an element of the actual prefab.

    So I try to make the bar look at the element which have the "player" tag but it don't work so i come here to get your help :)

    ps: sorry for my english i'm french so if u don't understand somethings tell me i can try explain differently :)
     
  2. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,847
    GameObject.Find is the naïve solution to this problem. Put it in Awake and cache the reference.

    If you are doing this a lot, you probably want something more complicated. But for now this will work.
     
  3. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,847
    One would assume you know the name of the player GameObject or the tag it has? The name of the GameObject is your key.
     
  4. Murgilod

    Murgilod

    Joined:
    Nov 12, 2013
    Posts:
    8,914
    Do you want it to look at the player or at the camera? If you just want it to face the camera, there's an example of how to do that using Transform.LookAt() in the docs: https://docs.unity3d.com/ScriptReference/Transform.LookAt.html
     
  5. NoxiFR

    NoxiFR

    Joined:
    Oct 19, 2022
    Posts:
    2
    Thanks I forgot this fonction
     
unityunity