How can I change other clients int health when I press space?

Options
I want to reduce other clients/players health when I press space, but I don't know how I could do this. Every tutorial is with a collider. I just can't figure out how I could do this by just pressing space or whatever Input.

Best Answer

Answers

  • Hi TheProphet,

    you can do this by sending RPCs or raise events on a given input. Therefore I strongly recommend you reading and following the tutorial over here. When implementing RPCs in this case, use PhotonTargets.Others instead of PhotonTargets.All (as stated in the tutorial) to only affect other players.
  • Thanks for replying Christian_Simon,

    I tried it, but it didn't work. Basically I have these two characters with their number of health on the screen and when I press space and do PhotonTargets.All, it reduces MY health and the other player can also see this and so can I, but when I do PhotonTargets.Others, then only the other player will see my health go down, but I won't.
    All I want to do is reduce other players health when I press space.

    this is the PunRPC code :

    [PunRPC]
    void reduceHealth()
    {

    health--;
    canvas.GetComponentInChildren().text = health.ToString();
    Debug.Log("REDUCEHEALTH");
    }
  • carmine
    Options
    When you send an RPC, you're sending it to you're own object's view. So in other words, you are sending it to other player's copy of YOUR object.

    I think what you're trying to do is, when you press space you want to send a message to all of the other views that are on the other objects.
  • After a bit of time looking into RPC calls and testing I figured it also out that I can only update MY player's variables to others, but I can't change OTHER player's health. Since my game is a very simple 2D fighting game I was hoping if there is a way to get a boolean from the other player to know when they are punching so I can reduce my own health and synchronize it with RPC or change THEIR health variable when I press space (punch animation).
  • jeanfabre
    Options
    Hi,

    I would suggest you follow the PUN basic tutorial and study the part where it deals with Health. You can do it either with RPC or indeed with a health variable being synchronised, the mean to affect health is independent from the way you are going to affect it in your game and interaction with the Players.

    https://doc.photonengine.com/en-us/pun/current/tutorials/pun-basics-tutorial/player-networking#health

    Bye,

    Jean