Know which player acts in multiplayer
Hello
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
Answer by toddisarockstar · Jan 04, 2016 at 08:25 PM
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 () {
if(Input.GetKeyDown("space")){
//----------------------------send---------------
networkView.RPC ("buttonpush", RPCMode.AllBuffered,1,myplayernumber,transform.position);}
}
//----------------------recieve---------------
@RPC
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.
Thank you so much It was exaclty this logic I needed.
One more question, when you say "future references between machines" you mean like if I want to destroy the game object I spawned? Will this array be useful then?
As far as the Network.Instantiate I found some tutorials that suggested to use Network Transform component on the game object I want to keep in sync. This works for me so far. I will keep your way in $$anonymous$$d if any problems emerge in synchronization.
Yeah, It totally depends on how complex your project is. The most important philosophy to understand getting into $$anonymous$$ultiplayer development is that even a smaller dual core CPU can think hundreds of times faster than data sent over a fast internet connection. So really most the information between computers is done mostly manually on a "need to know basis" for proficiency and any calculation is done on the local machine.
to answer your next question, thats just what i was talking about! if each spawned player is organized in a list or Array then yes, it would be very easy to Destroy something with a couple small ints with a RPC call. Better example might be using the arrays to do cool stuff like checking enemy distances, or updating rotations/animations based on position without the need to clog up precious bandwidth. actually, if you are at the step of moving characters now, you are going to find out in the next step that unity does not even offer a synchronization feature for animations! (but its for good reason).
you will eventually need arrays or similar databases to loop through for reference. when your character is spawned he might might also need a bazooka and a few grenades. which more importantly REQUIRE arrays to prevent need of synchronizing of those too!
Remember, anytime you sync an object, unity begins sending sets of 3 large float numbers approximately 60 times a second through our precious bandwidth for each object synced, which is a nice happy tool when necessary for our player characters.
for example... in most if games if a grenade is thrown, you don't use unity's sync features. A simple rpc is given only two times, once rpc signal for direction to give the others something to look at, and maybe a second RPC to update what the players machine said he blown up.
if you had a grenade cooking, each players machine would have the grenade position in the array changing, but only the player who owned it would give the RPC to start movment and blow it up. Assumption techics like this is why large commercial games like battlefield or call of duty can do so many players.
I noticed here, when you start asking multiplayer questions you dont get many answers. maybe because it is because there is a lot more opinions on different ways to do things depending on what you want to do.
anyways, what is your game setup? what are you trying to achieve in the end? I could provide code examples if i knew more. Its important to look at the "end picture" to set up any game from the beginning.
Ok I thought I had replied but can't see my reply (obviously I canceled). So let me start over again...
First of all thank you so much for your suggestions I will follow them and change the logic around communication in my project.
The project is a base deffence multiplayer game I am developing for my thesis in university. It is not too complex I think, but I am a begginer in networking so I am not familiar with this way of thinking.
It is 1 vs 1 game. One player creates a room and is host and local player at the same time and another can join the room.
There are two bases on the left and on the right one for every player. And there are buttons on screen for the players to hit and create their units (these buttons were my problem I had to detect which player hit the button to spaw the unit from his base and move it towards the opponents base).
Every unit starts from my base and moves until it collides with an enemy unit or my opponents base. If my unit collides with an enemy they fight until one is dead (every unit type has upgradable attack and defence stats). If a unit collides with the opponents base my opponent lose some of his life. When the player life is zero he loses the game.
I have my mechanics working (moment, animations, gold and mana changes etc) but as I said I have issues in communication. I tried to keep track of the player id via a static variable but this didn't work correctly. Now I am going to use your way with RPC calls. I will try this today (hopefully) and I will post my results and some of my code if needed.
Once again thank you for your time.
Your answer
![](https://koobas.hobune.stream/wayback/20220612054827im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Multiplayer - Error when rejoining room 1 Answer
Online client code execution 0 Answers
Multiplayer pass int from lobby to game scene 1 Answer
Match making in Photon 0 Answers
Spawning objects uNet Client and Local 0 Answers