Search Unity

Unet - Health not syncing

Discussion in 'UNet' started by warrencwwong, May 10, 2019.

  1. warrencwwong

    warrencwwong

    Joined:
    Aug 17, 2018
    Posts:
    25
    Hello, I am currently building a 2d multiplayer shooting game. I am trying to create a health script that syncs across every player's screen. But it is not working. These are my problems:
    1. My health bar is not syncing
    2. The bullets don't take damage for some players, but does for some. Why?
    This is my Health script:
    public Image fill;
    public int startingHealth;

    public GameObject DeadCanvas;


    public float currentHealth = 1;

    public bool Dead;

    void Start() {

    DeadCanvas = GameObject.Find("DeadCanvas");
    Dead = false;
    startingHealth = 100;
    currentHealth = 1;
    fill.fillAmount = 1;
    SetHealth();
    }


    void Update()
    {
    SetHealth();
    CheckDeath();
    }
    void CheckDeath()
    {
    if (currentHealth <= 0 && Dead != true)
    {
    Dead = true;
    GameManager.instance.onPlayerKilledCallback.Invoke(gameObject.name, "Enemy");
    GameObject DeadCanvas = GameObject.Find("DeadCanvas");

    gameObject.SetActive(false);
    }
    }

    public void TakeDamage(float dmg)
    {
    bool done = false;
    if (isServer)
    {
    RpcTakeDamage(dmg);
    return;
    done = true;
    }
    if (!isServer && !done)
    {
    CmdTakeDamage(dmg);
    }
    }
    void SetHealth()
    {
    if (!isLocalPlayer)
    {
    fill.fillAmount = currentHealth;
    }
    }

    [ClientRpc]
    void RpcTakeDamage(float damage)
    {
    if (!isServer)
    return;
    var Damage = damage / startingHealth;
    currentHealth -= Damage;
    fill.fillAmount = currentHealth;

    }
    [Command]
    void CmdTakeDamage(float damage)
    {

    var Damage = damage / startingHealth;
    currentHealth -= Damage;
    fill.fillAmount = currentHealth;
    }
    This is my bullet collision script(it's attached to the bullet)

    public float Damage = 0;

    public Vector3 dir;
    void Awake()
    {

    Destroy(gameObject, 2);
    }
    void FixedUpdate()
    {

    gameObject.GetComponent<Rigidbody2D>().AddForce(transform.right * 100);
    }
    void OnTriggerEnter2D(Collider2D col)
    {

    if (col.gameObject.GetComponent<Health>())
    {

    float health = col.gameObject.GetComponent<Health>().currentHealth / 100;
    col.gameObject.GetComponent<Health>().TakeDamage(Damage);

    This part is basically checking if player has won, so don't worry if it looks confusing...:
    if (col.gameObject.GetComponent<Health>().currentHealth < health)
    {

    if (GameObject.Find(gameObject.name).GetComponent<PlayerMovement>()()
    {

    GameObject.Find(gameObject.name).GetComponent<PlayerMovement>().HasWon = true;
    }
    }
    Destroy(gameObject, 0.01f);
    }

    }
    I have already seen a lot of sites but none of them works! So please help me! Thanks.
     
    Last edited: May 12, 2019
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Post code using CODE tags so people can actually read it.

    This looks like it is using Unet, but your code doesn't make much sense. currentHealth and Dead look like things you should be syncing between the server and clients, but you're not. You're also calling Commands without first checking if that is possible on this client (so your clients probably have a lot of warnings about lack of authority to call a Command).

    Then you're doing things like calling RpcTakeDamage, which as a ClientRpc is something you'd call on the server to execute on all clients, but in RpcTakeDamage the first thing you do is return if the client isn't the server - well the client probably isn't the server. Why even make this a ClientRpc if you want to return if it isn't the server?

    Go through your code and think about what variables are what on the clients and the server (because they are separate unless you sync them from the server to the clients), look at what code is being run on the server vs what you're running on the clients, and you should be able to figure out all these issues. Add Debug.Log statements everywhere so you can track what your code is doing as it happens.

    Also, Unet is dead. If you are this early in your game development you should drop Unet and move to a not so dead network API.
     
    Last edited: May 13, 2019
  3. Munchy2007

    Munchy2007

    Joined:
    Jun 16, 2013
    Posts:
    1,735
    So much pessimism in that statement :p :)
     
    Joe-Censored likes this.
  4. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    It is just getting a little frustrating seeing new threads every day from people who are obviously just starting or very early in their network game project, and the most important API for the project they are choosing one which is buggy, never finished, deprecated a year ago, and produces 404 errors when you try to go to many of its scripting reference pages (because of the whole removed/deprecated thing).
     
    Munchy2007 likes this.
  5. Munchy2007

    Munchy2007

    Joined:
    Jun 16, 2013
    Posts:
    1,735
    I couldn't agree more.
     
    Joe-Censored likes this.
  6. warrencwwong

    warrencwwong

    Joined:
    Aug 17, 2018
    Posts:
    25
    So what do you think people should use? Photon?
     
  7. Munchy2007

    Munchy2007

    Joined:
    Jun 16, 2013
    Posts:
    1,735
    Only if it suits the type of game you're making, you need to look at what you want from it and if its feature set meets your requirements then yes by all means use it.

    Otherwise go with one of the other UNET alternatives that does do what you want.