- Home /
Instantiate health bar with networking
When a bullet hit an asteroid, the health bar decrease of 10 if the player is only local, so how Instantiate the healthbar on the server, this is my scripts : using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.Networking;
public class AsteroidController025 : NetworkBehaviour {
[SerializeField]
private StatA asteroidHealth025;
private void Awake()
{
asteroidHealth025.Initialize();
}
void OnTriggerEnter2D(Collider2D other)
{
if (other.gameObject.tag == "Bullet")
{
asteroidHealth025.CurrentVal -= 10;
Destroy(other.gameObject);
}
}
// Use this for initialization
void Start()
{
}
// Update is called once per frame
void Update()
{
if (asteroidHealth025.CurrentVal <= 0)
{
Destroy(this.gameObject);
}
}
} and
using System; using System.Collections; using UnityEngine; using UnityEngine.Networking;
[Serializable] public class StatA { [SerializeField] private ABarScript aze;
[SerializeField]
private float maxVal;
[SerializeField]
private float currentVal;
public float CurrentVal
{
get
{
return currentVal;
}
set
{
this.currentVal = Mathf.Clamp(value, 0, MaxVal);
aze.Value = currentVal;
}
}
public float MaxVal
{
get
{
return maxVal;
}
set
{
this.maxVal = value;
aze.MaxValue = maxVal;
}
}
public void Initialize()
{
this.MaxVal = maxVal;
this.CurrentVal = currentVal;
}
}
Answer by Shrikky23 · Jul 04, 2017 at 03:00 PM
Okay I see what is going on. First I will assume you know how Unity Networking works (How the Sync between player objects work, what is network behavior etc)
To Solve your specific problem, I noticed that your StatA script is not inheriting from Network behavior, which means it cannot run any network specific functionalities in the script itself. A script has to inherit from NetworkBehavior and have a NetworkIdentity component to be spawned in the network as you wanted. If you use NetworkServer.Spawn (called in the server/host), then one instance of the object will be spawned in all the player's scenes and it will have a NetworkInstanceID attached and other properties like "isLocalPlayer", "HasAuthority?" etc, however, the ownership of the object belongs to the person who spawned it, in our case it would be server/host.
There are cool Unity Network attributes like [SyncVar] it is used to synchronize variables across its instances in the network, which means if you add [SyncVar] above private float currentVal; it will synchronize its value automatically across all the network players. You must also understand that only the player who spawned the Object can control or change its Values (Look more into Object Authority/ who has the right to modify values)
Unity Networking is fun and time consuming at the same time. I kept going back to read networking documentation so often to understand how Command/RPC/ Local Authority/ Synchronization of variables using SyncVar, SyncEvent and how all of that works.
I hope my explanation gives a good start for your problem.
So, I can't sync the health bar on all screen ? The sync var don't works, when it's on, the healthbar has a value = 0. if you can, send me the script changed please. And I forget the last script : using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;
public class BarScript : $$anonymous$$onoBehaviour {
private float fillAmount;
[SerializeField]
private float lerpSpeed;
[SerializeField]
private Image content;
[SerializeField]
private Text valueText;
public float $$anonymous$$axValue { get; set; }
public float Value
{
set
{
string[] tmp = valueText.text.Split(':');
valueText.text = tmp[0] + ": " + value;
fillAmount = $$anonymous$$ap(value, 0, $$anonymous$$axValue, 0, 1);
}
}
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
HandleBar();
}
private void HandleBar()
{
if (fillAmount != content.fillAmount)
{
content.fillAmount = $$anonymous$$athf.Lerp(content.fillAmount, fillAmount, Time.deltaTime * lerpSpeed);
}
}
private float $$anonymous$$ap(float value, float in$$anonymous$$in, float in$$anonymous$$ax, float out$$anonymous$$in, float out$$anonymous$$ax)
{
return (value - in$$anonymous$$in) * (out$$anonymous$$ax - out$$anonymous$$in) / (in$$anonymous$$ax - in$$anonymous$$in) + out$$anonymous$$in;
}
}
$$anonymous$$ake your fillamount a syncvar? So networking debugging is fun, change sync var and see if it displays on your other player's screen. If it changes, then all you have to do is apply the new fill amount to the content.fillAmount using your handlebar function