Hi. I try create HP bar to unit, but again and again something wrong.
2 scripts on Player
create hp bar
public static GameObject playerHPpanel;
static UIHealthInfo _UIHealthInfo;
// Use this for initialization
void Start()
{
if (tno.isMine)
{
// not network sample
// GameObject slider = Instantiate<GameObject>(Resources.Load<GameObject>("HPpanelUI"));
// slider.transform.SetParent(GameObject.Find("MainCanvas").transform);
// _UIHealthInfo = slider.GetComponent<UIHealthInfo>();
// _UIHealthInfo.Target = this;
// network sample
Vector3 pos = new Vector3(37.0f, 4, 38);
Quaternion rot = Quaternion.Euler(0, 45, 0);
TNManager.Instantiate(2, "MenuCreate", "HPpanelUI", false, pos, rot);
_UIHealthInfo.Target = this;
}
}
[RCC]
static GameObject MenuCreate(GameObject prefab, Vector3 pos, Quaternion rot)
{
// Instantiate the prefab
playerHPpanel = prefab.Instantiate();
// Set the position and rotation based on the passed values
Transform t = playerHPpanel.transform;
t.position = pos;
t.rotation = rot;
playerHPpanel.transform.SetParent(GameObject.Find("MainCanvas").transform);
_UIHealthInfo = playerHPpanel.GetComponent<UIHealthInfo>();
return playerHPpanel;
}
Next script change position.
public class UIHealthInfo : MonoBehaviour {
public HealthInfo Target { get; internal set; }
// Use this for initialization
UIHealthInfo _UIHealthInfo;
void Start () {
}
void Update()
{
if (Target == null)
return;
Vector3 oldPos = new Vector3(Target.transform.position.x, Target.transform.position.y + Target.GetComponent<Collider>().bounds.size.y*3, Target.transform.position.z);
Vector3 newPos = Camera.main.WorldToScreenPoint(oldPos);
GetComponent<RectTransform>().position = newPos;
}
}
its work in single, but in multiplayer something wrong, maybe problem with Target.
I try to go on easy way also, i off it all, and create Canvas into player unit. Its have problem too.
Function DestroySelf - work and destroy unit, but after added Canvas with hp panel, DestroySelf 100% crush Unity or game.