Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question UI health bar in world for multiple players

Discussion in 'Netcode for GameObjects' started by Bast75, Jul 19, 2023.

  1. Bast75


    Dec 3, 2017
    Hi all,
    How can i create a in world health bar that is rotated the correct way for each online player?

    For example a game object with a health bar is right between two online players with each their own camera. How can i fix it so all players see the health bar correct?

    With a single player i just rotate it to the camera, but with multi player there are more camera's...
  2. jackward84


    Jan 26, 2017
    Have a game object with a world canvas on it attached to each player that is not a network object.

    Then rotate that to look at Camera.main (or whatever camera you're using). There's no reason the local client should even know where other players' cameras are.

    It's as simple as just attaching this to the world space canvas of every player object. If you are having issues I would make sure that the component isn't a networkbehaviour or something.

    Code (CSharp):
    1.     void LateUpdate()
    2.     {
    3.         if(Camera.main == null)
    4.         {
    5.             return;
    6.         }
    7.         transform.LookAt(transform.position + Camera.main.transform.forward);
    8.     }
  3. Bast75


    Dec 3, 2017
    I forgot that not everything in a multi player game must be synced over the network.
    As you said: the the canvas object just need to be rotated locally on a client, no sync needed. Cameras doesnt need sync too, players dont have to see where the other players is looking at, specially in an rts.

    But thanks for hint, i will try it soon.
    I was way overthinking the problem.