#Working on worldspace healthbars

1 messages · Page 1 of 1 (latest)

south locust
#

ill send scripts here in a minute

#

using UnityEngine;

using UnityEngine;

public class Billboard : MonoBehaviour
{
private Camera cam;

private void LateUpdate()
{
    if (cam == null)
        cam = Camera.main;
    if (cam == null) 
        return;

    Vector3 dir = cam.transform.position - transform.position;
    dir.y = 0f;
    transform.rotation = Quaternion.LookRotation(-dir);
}

}

using UnityEngine;
using UnityEngine.UI;

public class HealthBarUI : MonoBehaviour
{
[SerializeField] private Image fillImage;

public void SetHealth(float current, float max)
{
    float pct = Mathf.Clamp01(current / max);
    fillImage.fillAmount = pct;
}

}

#

@vivid meteor

#

sorry these are older, im updating it

#

now theyre up to date

left crow
#

What's the issue you are having with it?

south locust
#

it "works" but it doesnt. the first player who loads in has the effect that is intended but the second player doesnt

#

ill send a video

left crow
#

Make sure there is only one active camera in the scene.

south locust
#

the cameras are activated based on ownership

left crow
#

make the buillboard a network behavior, then you can set the camera transform in OnNetworkSpawn()

south locust
#

why didnt i think of that

left crow
#

I would also disable the camera based on IsLocalPlayer instead of ownership.

south locust
#

whats the difference?

left crow
#

ownership sometimes takes a frame after spawning to get set properly

south locust
#

is this the proper setup?

#

im pretty new with this sort of thing

left crow
#

actually using transform.LookAt(Camera.main.transform.position) in LateUpdate() should just work. With no need to put anything in OnNetworkSpawn()

south locust
#

let me try that

#

oh its so close to working

#

using UnityEngine;

public class Billboard : MonoBehaviour
{
private Camera cam;

private void LateUpdate()
{
    if (cam == null)
        cam = GetLocalCamera();
    if (cam == null) return;

    transform.LookAt(cam.transform.position);
}

private Camera GetLocalCamera()
{
    // Find the camera that belongs to the local player
    foreach (var c in Camera.allCameras)
    {
        if (c.enabled && c.CompareTag("MainCamera"))
            return c;
    }
    return null;
}

}

left crow
#

the only line you need is transform.LookAt(Camera.main.transform.position) in LateUpdate().
no need to loop through inactive cameras

vivid meteor
south locust
#

i dont really know how to do it any other way

vivid meteor
#

the easiest/simplest way is to use cinemachine, give the player object a CinemachineCamera. And then have a NetworkBehaviour on the player object just do something like:

void OnNetworkSpawn() {
  if (!IsOwner) myCinemachineCamera.enabled = false;
}```
#

and then as usual with Cinemachine you only have one Unity camera in the scene

south locust
#

i could try that real quick

left crow
#

that works too, if you are using cinemachine

south locust
#

so using that kind of setup with cinemachine should fix it right away?

left crow
#

I dont think your issue is with cameras

south locust
#

it might be

#

the system works as intended only for whoever connects first

#

im gonna try cinemachine real quick

#

yeah it didnt work, this is my camera ownership block

#

using UnityEngine;
using PurrNet;

public class Billboard : NetworkBehaviour
{
private Camera cam;

private void LateUpdate()
{
    if (cam == null)
        cam = Camera.main;
    if (cam == null) return;

    transform.LookAt(cam.transform.position);
}

}

#

fuck i broke it again ;-;

#

the players see a side bar of their health

left crow
#

the only line you need in LateUpdate is transform.LookAt(Camera.main.transform.position)
this will always point the gameobject to the active camera

south locust
left crow
#

Camera.main.trainsform.position

#

also just noticed that you are using Purrnet. I've been assuming you were using NGO.

south locust
#

purrnet has been doing really well but some things don’t have a lot of support

#

using UnityEngine;
using PurrNet;

public class Billboard : NetworkBehaviour
{
private Camera cam;

private void LateUpdate()
{
    transform.LookAt(Camera.main.transform.position);
}

}
so it should look like this?

left crow
#

yea.

south locust
#

alright ill try that

#

its just that stupid extra bar thats attached

#

so now both players are getting the intended effect, they just have that thing off to the side of them

left crow
#

do you see that bar in the scene view? what object is it attached to?

south locust
#

ill get a video

#

maybe it needs to be locked into a specific position

#

the players cant see each others "sidebar", only the owner of that camera does

#

actually maybe i just need to make it a layer and make it to where the local player cant see that layer

left crow
#

Oh that is your own bar. No clue why it's showing there and not above your player

#

Yea your own bar should be disabled. But the canvas should be on a Gameobject. It shouldn't have a rect transform

south locust
#

i dont think i understand

#

i cant remove the rect from the canvas

left crow
south locust
#

nvm its not what youre suggesting

#

how would the references work then?

left crow
#

I mean parenting the canvas to a gameobject then parenting that gameobject to the player. that middle gameobject would have the billboard script on it. I think rotating the canvas directly might be causing your issue

#

I'll show you how I did mine later when I get off work

south locust
south locust