How to Send a RPC to server to update other players health

As the tittle says, what code would i put into the Update code.

i have health bars above every player and i need their hp bars set to what their life actually is so other players know how much life they have.

i have this at the moment that damages the player if he collides with a cube.

public class Dammage : Photon.MonoBehaviour {
	public Slider healthBarSlider;

	void OnTriggerStay(Collider other){
				if (other.gameObject.name == "Pain" && healthBarSlider.value > 0) {
						healthBarSlider.value -= .10f;  //reduce health
				}
		}
		[RPC] void Update(){
			//this part is what i am confused about.
		}

}

3 Answers

3

Once player Hit, you need to call RPC function.

private float m_Health;
	private void OnTriggerEnter(Collider other) 
	{
		networkView.RPC ("ReduceHealth", RPCMode.All); // Call to RPC function
	}

	[RPC]
	public void ReduceHealth()
	{
		m_Health --;
	}

Read Unity Class reference for more info

sorry i forgot to mention that i am using photon i thought that it was there, well it says it "Photon.MonoBehaviour" just not stated

In Photon unity networking, private float m_Health; private void OnTriggerEnter(Collider other) { PhotonView photonView = this.photonView; photonView.RPC("ReduceHealth", RPCMode.All); // Call to RPC function } [RPC] public void ReduceHealth() { m_Health --; } Check [Photon Class Reference][1] [1]: http://doc-api.exitgames.com/en/realtime/current/pun/doc/class_photon_network.html

apparently it needs another parameter or something "The best overloaded method match for `PhotonView.RPC(string, PhotonPlayer, params object[])' has some invalid arguments" this damage script is attached to the player prefab

also i need new players that join the game to be updated with other players current health

Oh sorry, Not RPCMode, should be "PhotonTargets" :) private float m_Health; private void OnTriggerEnter(Collider other) { PhotonView photonView = this.photonView; photonView.RPC("ReduceHealth", PhotonTargets.All); // Call to RPC function } [RPC] public void ReduceHealth() { m_Health --; }

i modified some of that i understand now how it works, thanks man this was my final code

using UnityEngine.UI;
using UnityEngine;
using System.Collections;

public class Dammage : Photon.MonoBehaviour {
	public Slider healthBarSlider;
	public GameObject player;

	public void Reduce(float p){
		healthBarSlider.value -= p;
		Debug.Log (p + " " + healthBarSlider.value);
		}
	private void OnTriggerEnter(Collider other) 
	{
		PhotonView photonView = this.photonView;
		photonView.RPC("ReduceHealth", PhotonTargets.AllBufferedViaServer); // Call to RPC function
	}
	
	[RPC]
	public void ReduceHealth()
	{
		Reduce(10);
	}

}

hi how do i apply this to weapon switch. this is my code. im confused with this rpc thing

public int selectWeapon = 0;

void Start()
{

    SelectWeapon();

}

void Update()
{
    PhotonView Photonview = this.photonView;

    SelectWeapon();

    if (Input.GetAxis("Mouse ScrollWheel") > 0f)
    {
        CallRemoteMethod();

        if (selectWeapon >= transform.childCount - 1)

            selectWeapon = 0;

        else

            selectWeapon++;
    }

    if (Input.GetAxis("Mouse ScrollWheel") < 0f)
    {
        CallRemoteMethod();
        if (selectWeapon <= 0)

            selectWeapon = transform.childCount - 1;

        else

            selectWeapon--;
    }

    if (Input.GetKeyDown(KeyCode.Alpha1))
    {
        selectWeapon = 0;

    }

    if (Input.GetKeyDown(KeyCode.Alpha2))
    {
        selectWeapon = 1;

    }
    if (Input.GetKeyDown(KeyCode.Alpha3))
    {
        selectWeapon = 2;

    }

}

public void CallRemoteMethod()

{
    GetComponent<PhotonView>().RPC("SelectWeapon",
                                    PhotonTargets.AllBuffered);

}


public void SelectWeapon()
{
    int i = 0;
    foreach (Transform weapon in transform)
    {
        if (i == selectWeapon)
            weapon.gameObject.SetActive(true);
        else

            weapon.gameObject.SetActive(false);

        i++;
    }

}