- Home /
How to differentiate between players in multiplayer game
So I am trying to make a multiplayer game, and the thing is how do I identify one player and another one. What I mean is I have a class with the player stats, such as health and attack for now. I spawn the player when they are connected to the server with a player prefab (just a cube with movement, and the stats). I'm trying so if a player right clicks it'll just do damage to anyone in front of it one time just for testing purposes. I do not know how to go to that specific player's class and say doDamage() to the other person and how i get the other person's specific stats. I just need a small example of how to do this thanks a lot!
Answer by Em3rgency · Jun 21, 2015 at 07:16 AM
Keep in mind that multiplayer games are significantly harder to make than single player games. I suggest you learn how to do something in single mode before trying to tackle it in multiplayer.
Now as for your problem. You don't need to specify a player in code, when doing things just with that player. So for example if I had a LevelUp() function, and I called it for an object, unity would automatically only level up the specific player object that called it, not all the players. No additional handling needed.
Now when doing things like damaging other objects, you will get the object reference when checking if you hit something. For example, if you want to hit something in front of you, you will probably perform a raycast (look it up) to see if there is anything in front. And the raycast WILL return the object it hits, so you can call TakeDamage(amount) or whatever on that object.
Once again, doing this in multiplayer will be harder, as you will have to add in an additional layer of code, to handle the information exchange between the server and clients. And it also depends on what kind of networking setup you will be going for. Server authoritative code would look different from client trust (client authoritative? Don't know what the term is) code.
Your answer
Follow this Question
Related Questions
Multiplayer problem - 2nd player erratic movment? 0 Answers
How to make multiplayer editor/race 0 Answers
How to check which gameobject is the player using 2 Answers
unity game 1 Answer