Know which player acts in multiplayer


I am developing a 2 player game using Unity Networking (players are on different pc)

Player 1 is both host and local player

Player 2 is just a client

What I am trying to achieve is

  • When P1 hits the button on screen the gameObject spawns on the left and moves to the right

  • When P2 hits the button on screen the gameObject spawns on the right and moves to the left

Is there a way to know the player who hits the button everytime? And spawn the object from the right place?

(It is not turn based a player can hit more than one times the button before the opponent)

Thank you

networking projects are a bit more complex. when a button is pushed, that player’s machine would need to send an “RPC call” to the other players. the RPC would contain info like a button number and a player number and maybe a location numbers.

you would setup a receiving section of code in your script to grab the incoming RPC info from the other machines and they would each spawn and do whatever based on the info they receive.

here is a simple RPC example:

var myplayernumber:int;

function Update () {
networkView.RPC ("buttonpush", RPCMode.AllBuffered,1,myplayernumber,transform.position);}

function buttonpush (info1:int,info2:int,info3:Vector3){

print("All machines know that player"+info2+" pushed a button at this location "+info3);

there is also Network.Instantiate and state synchronization options if you are looking to update locations on a frame by frame basis. but one way or another, anytime you are spawning stuff on a multiplayer game you should have a way of login stuff into arrays for future reference
between machines.