Move another player via RPC

Options
Hello,

from a specific player I want to change the position of another player. In order to achieve my goal I'm using the following RPC call:

m_PhotonView.RPC("changePosition", PhotonTargets.All, name, vector[1], vector[2], vector[3], vector[4]);


The changePosition method now checks if the passed name parameter equals the name of the PhotonView.
[PunRPC] void changePosition(string name, string x, string y, string z, string rotY) { if (PhotonNetwork.playerName.Equals("Player " + name)) { Debug.Log(PhotonNetwork.playerName + " " + transform.position); transform.position = new Vector3(float.Parse(x), float.Parse(y), float.Parse(z)); Debug.Log(transform.position); } else { return; } }

The code fragments above are in the same script, which is attached to each player object.
But sadly the only moving player is the one which has called the RPC. In the Debug messages above I can see that the position is changing. But some process seems to set the position back to the startposition.
Here you can see my playercreation code:

void CreatePlayerObject(string name) { Vector3 position = new Vector3(33.5f, 1.5f, 20.5f); GameObject newPlayerObject = PhotonNetwork.Instantiate("Robot Kyle RPG", position, Quaternion.identity, 0); PhotonNetwork.playerName = "Player " + name; newPlayerObject.name = "Player " + name; newPlayerObject.GetComponent<RPGMovement>().setName(name); Camera.Target = newPlayerObject.transform; }

Thank you very much for your help!!!

Comments

  • PhilippL
    Options
    Update:
    [PunRPC] void changePosition(string name, string x, string y, string z, string rotY) { if (PhotonView.Get(this).owner.name.Equals("Player " + name)) { PhotonView.Get(this).gameObject.transform.position = new Vector3(float.Parse(x), float.Parse(y), float.Parse(z)); } else { return; } }

    Unfortunately only the player which calls the RPC is moving... What am I doing wrong :-(
  • vadim
    Options
    Make sure that you call RPC on object representing another player in scene, not on the 'specific' player.
    Also send RPC to owner of this another player object only to save bandwidth and couple lines of code (you do not need to do IsMine check in RPC method n this case).
    Avoid using playerName for game logic. Use PhotonPlayer reference or actorId (PhotonView.owner and PhotonView.ownerId) to uniquely identify client.
  • PhilippL
    Options
    Thank you for your answer. The following code worked. But I know that it isn't the best practise. I will try your solution!!!
    [PunRPC] void changePosition(string name, string x, string y, string z) { GameObject player = GameObject.Find("Player " + name); if (player != null) { player.transform.position = new Vector3(float.Parse(x), float.Parse(y), float.Parse(z)); } }
  • PhilippL
    Options
    Also send RPC to owner of this another player object only to save bandwidth and couple lines of code (you do not need to do IsMine check in RPC method n this case).
    How do I send an RPC to one specific player?
  • vadim
    Options
    Specify target PhotonPlayer in 3rd parameter of PhotonNetwork.RPC call