Reduce other player's health via photonView.RPC() [SOLVED]

Hi Everyone,

I spent a few hours trying to scour as much info about this and I wanted to know if PhotonView has a method to target a specific object that contains a PhotonView, for example a player or and an enemy.

Here's what I'm trying to do:
1. Player fires bullet at enemy.
2. Enemy sustains damage by -10.
3. Player status shows + 10 points.
4. Enemy status show -10 points.

(The problem I found was that the Player score and Enemy damage were displaying as the same value.)

One way I thought of doing this was via an OnTriggerEnter event. I would acquire the other Collider value (from the bullet object) and based on what object collided (for example the enemy), a photonView.RPC() would be sent to the enemy that collided. The attached behaviour on the enemy object would reduce it's point based on the damaged passed via a parameter.

For example, something like this:
//damage.js file
photonView.RCP("ApplyDamage, PhotonTargets.All, 10.0);

The problem for me is that I am not sure how to effectively acquire the object that I want the damage applied to.

In Unity networking, they had something like this:
other.gameObject.networkView.RPC("ApplyDamage", RPCMode.All, 10.0);

I'm looking for an PUN equivalent. If there is a better way to do this, please advise.

Thanks for your help. :D

Comments

  • Amazing what a few hours of sleep can do. When I stare at the same code, it sometimes becomes a situation of diminishing returns.

    I got this solved. PUN was doing exactly what it was required to do. The problem? I was sharing the same variable on the label that displayed the player's status.

    So the takeaway for me was:
    1. Variables and what object owns them is very important.
    2. Keep the RPC call and the [RPC] or @RPC in the same file and attached to the object that is being affected or that needs to broadcast out to other players to let them know what just happened.

    So in short, no PUN equivalent required. Just sleep.
  • I'm glad you found the solution even though you spent a few hours on it.
    RPCs target the exact GameObject they were fired on. Unless you explicitly find another GameObject in your RPC code, an RPC won't affect anything else.

    I'm sorry this wasn't clear to begin with. I know I wrote it several times somewhere but maybe not where you looked it up.
  • Thanks Tobias, I'm realizing much of my time is spent looking, then having to sift through hundreds and hundreds of potential answers.