Search Unity

Third Party Photon Network - update Character changes over network

Discussion in 'Multiplayer' started by Krisper, Oct 5, 2014.

  1. Krisper

    Krisper

    Joined:
    Mar 24, 2011
    Posts:
    20
    I am having trouble getting my head around a networking issue. I am using Photon Network and using various tutorials etc I have my game so the characters animations etc are updated over the network using OnPhotonSerializeView.

    I have been trying for ages to figure out how to make changes I make to my character show up on the other players view. For example I might change my hair colour by changing the material on the Hair. I have tried many ways of trying to get the material to change on the same character on other players views. But I can't get it to work. It always ends up that the players characters hair changes on both games running, rather than the one character.

    Can someone give me some hints how I can send this change across the network?
     
  2. Krisper

    Krisper

    Joined:
    Mar 24, 2011
    Posts:
    20
    Still trying to figure this out without luck. So I have a script to control my character. Here is some snippets of the script:-

    Code (CSharp):
    1.    void Update () {
    2.         if(isControllable) {
    3.             GetInput();
    4.         }
    5.     }
    6.  
    7.     void GetInput() {
    8.         if (Input.GetKeyUp(KeyCode.Alpha1)) {
    9.             ChangeHair(1);
    10.         } else if (Input.GetKeyUp(KeyCode.Alpha2)) {
    11.             ChangeHair(2);
    12.         } else if (Input.GetKeyUp(KeyCode.Alpha3)) {
    13.             ChangeHair(3);
    14.         } else if (Input.GetKeyUp(KeyCode.Alpha4)) {
    15.             ChangeHair(4);
    16.         } else if (Input.GetKeyUp(KeyCode.Alpha5)) {
    17.             ChangeHair(5);
    18.         }
    19.     }
    20.  
    21.     void ChangeHair(int number) {
    22.         if(number == 1) {
    23.             hair.renderer.material = blackHair;
    24.         } else if(number == 2) {
    25.             hair.renderer.material = brownHair;
    26.         } else if(number == 3) {
    27.             hair.renderer.material = redHair;
    28.         } else if(number == 4) {
    29.             hair.renderer.material = whiteHair;
    30.         } else if(number == 5) {
    31.             hair.renderer.material = yellowHair;
    32.         }
    33.         photonView.RPC("ChangeHairColour", PhotonTargets.All, hairNumber);
    34.     }
    35.  
    36.     [RPC]
    37.     void ChangeHairColour(int hairNum) {
    38.         if(hairNum == 1) {
    39.             hair.renderer.material = blackHair;
    40.         } else if(hairNum == 2) {
    41.             hair.renderer.material = brownHair;
    42.         } else if(hairNum == 3) {
    43.             hair.renderer.material = redHair;
    44.         } else if(hairNum == 4) {
    45.             hair.renderer.material = whiteHair;
    46.         } else if(hairNum == 5) {
    47.             hair.renderer.material = yellowHair;
    48.         }
    49.     }
    50.  
    So I run 2 instances of the game. I have one character names Bob and one named Bill. When Bill changes his hair colour, in the other game Bob's hair changes. And when Bob changes his hair, in the other game Bill's hair changes. I can't get any characters apart from the controlled one in each game to change.

    Can someone help with this?
     
  3. tobiass

    tobiass

    Joined:
    Apr 7, 2009
    Posts:
    3,066
    I assume both instances of characters have the same scripts? So unless you disable one of those scripts, both with react to input. Both will ChangeHair when you press the fitting keys.

    One character on both clients is owned by the other, remote client. Every client only sends updates for PhotonViews it owns. In turn, this means that changes of a "copy" are not sent around. The owner might send another update overriding whatever color you set.

    You need to enable and disable the scripts that get input and modify "owned" GameObjects.
     
  4. Krisper

    Krisper

    Joined:
    Mar 24, 2011
    Posts:
    20
    Hi Tobias, thanks for the reply, yes I do only allow input from the owning script. I only get the input when isControllable is true. This is only set for the owners character.

    What I have found is that it must have something to do with changing the rendering. Because I can use the same sort of code for a sword strike, and the correct character will swing his sword. But as soon as I try to change the rendering on the model, nothing happens on the network character, only on the local one.