#Working on worldspace healthbars
1 messages · Page 1 of 1 (latest)
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
What's the issue you are having with it?
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
Make sure there is only one active camera in the scene.
make the buillboard a network behavior, then you can set the camera transform in OnNetworkSpawn()
why didnt i think of that
I would also disable the camera based on IsLocalPlayer instead of ownership.
whats the difference?
ownership sometimes takes a frame after spawning to get set properly
actually using transform.LookAt(Camera.main.transform.position) in LateUpdate() should just work. With no need to put anything in OnNetworkSpawn()
let me try that
oh its so close to working
@left crow
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;
}
}
the only line you need is transform.LookAt(Camera.main.transform.position) in LateUpdate().
no need to loop through inactive cameras
why would you even have more than one camera
each player gets their own camera
i dont really know how to do it any other way
You only need a camera for the local player in a networked game
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
i could try that real quick
that works too, if you are using cinemachine
so using that kind of setup with cinemachine should fix it right away?
I dont think your issue is with cameras
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
wtf is causing this?
the second players healthbar is being rotated towards the main player
the only line you need in LateUpdate is transform.LookAt(Camera.main.transform.position)
this will always point the gameobject to the active camera
using UnityEngine;
using PurrNet;
public class Billboard : NetworkBehaviour
{
private Camera cam;
private void LateUpdate()
{
transform.LookAt(cam.transform.position);
}
}
?
Camera.main.trainsform.position
also just noticed that you are using Purrnet. I've been assuming you were using NGO.
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?
yea.
alright ill try that
its so close to working
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
do you see that bar in the scene view? what object is it attached to?
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
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
i dont think i understand
i cant remove the rect from the canvas
is this what you're suggesting?
put the canvas on a separate gameobject instead of directly on the player.
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
you are very appreciated! thank you so much, im so much closer to getting it working
im trying culling masks and it's almost working properly
IT WORKS