Domanda

I'm having a difficult time getting my health bar to sync through the server on Unity using RPCs. In my game, the characters have health bars over their heads that are supposed to update for the entire server. That way you can just look at another player and see their health bar. The problem is that even though I send the information through the network and it's received, the actual physical bar doesn't change in scale. The player who sends the call has their bar changed though.

Here's a screenshot of the issue: http://i.imgur.com/g2GozZv.png

When I send the RPC, It does change the other player's health value, but does nothing to the scale.

I did the following code and it doesn't work:

void Start()
{
    if(!networkView.isMine)
    {
        enabled = false;
    }
}

void Update ()
{
    if(Input.GetKey(KeyCode.Alpha2))
    {
         Minus_Health();
    }
}

public void Minus_Health()
{
    health -= 10;
    healthBarLength = (float)health / (float)maxHealth / 5.1f;
    healthBar.scale = new Vector2(healthBarLength, healthBar.scale.y);
    Update_HP(health, maxHealth, healthBar.scale);
}

public void Update_HP(int hp, int maxHP, Vector3 bar)
{
    networkView.RPC("Update_Health",RPCMode.All, hp, maxHP, bar);
}


[RPC]
public void Update_Health(int value, int value2, Vector3 bar)
{
    health = value;
    maxHealth = value2;
    healthBar.scale = new Vector2(bar.x, bar.y);
}

I've also tried this, with no luck either:

void OnSerializeNetworkView(BitStream stream, NetworkMessageInfo info)
{
    if (stream.isWriting)
    {
        Vector3 networkHealth = healthBar.scale;
        stream.Serialize(ref networkHealth);
    }
    else
    {
        Vector3 networkHealth = Vector3.zero;
        stream.Serialize(ref networkHealth);
        healthBar.scale = networkHealth;
    }
}
È stato utile?

Soluzione

I found the problem. The code itself was working great (and you're right pek, that bar parameter was just tedious).

The problem was actually with the ex2D plugin I was using for the health bar. On each exSprite there is a camera view, set to the user's main camera. Because it was set to my camera on player instantiate, it was only seeing my bar through my camera, thus not updating the other bar via client / server side. By clicking on the texture and setting the ex2D exSprite's Camera to None, I can now see both bars being updated / scaled properly.

In hopes that this can help anyone looking for how to do health bars over a network, here is the final code I'm using:

using UnityEngine;
using System.Collections;

public class PlayerStats : MonoBehaviour
{
    public int health;
    public int maxHealth;
    public float healthBarLength = 0;
    public exSprite healthBar;

    void Start()
    {
        if(!networkView.isMine)
        {
            enabled = false;
        }
    }

    void Update ()
    {
        if(Input.GetKey(KeyCode.Alpha2))
        {
            Minus_Health();
            Update_HP(health, maxHealth);
        }
    }

    public void Minus_Health()
    {
        health -= 10;
    }

    public void Update_HP(int hp, int maxHP)
    {
        networkView.RPC("Update_Health", RPCMode.AllBuffered, hp, maxHP);
    }


    [RPC]
    public void Update_Health(int value, int value2)
    {
        health = value;
        maxHealth = value2;

        healthBarLength = (float)value / (float)value2 / 5.1f;
        healthBar.scale = new Vector2(healthBarLength, healthBar.scale.y);
    }
}

Also, a small tip for those who get errors when setting the exSprite Camera to None; You need to update your ex2D plugin.

Thanks for the help pek and I hope this can help someone! :)

Altri suggerimenti

If all the parameters in Update Health are correct, then there might be something else that is affecting the scale.

Also, if value and value2 are sent correctly, then there is no need for the bar parameter:

[RPC]
public void Update_Health(int value, int value2)
{
    health = value;
    maxHealth = value2;

    healthBarLength = (float)health / (float)maxHealth / 5.1f;
    healthBar.scale = new Vector2(healthBarLength, healthBar.scale.y);
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top