- Home /
How can assign just 1 specific prefab to 1 specific player?(Multiplayer)
Hi, i'm having a lot of troubles to get the basis of my game to work correctly, and i think that is because i'm trying to run all players with the same prefab. You see..., 1 player controls all the players (mouselook, movement, even the pause menu, etc), so i tried converting my scripts to Multiplayer using codes such as:
function Awake ()
{
if(!networkView.isMine)
{
enabled=false;
}
else
{
enabled=true;
}
}
In the example above, i applied the code (in a specific script) to the main camera of my unique prefab which i'm trying to instantiate and control properly in a server, but as soon as i try to connect from a standalone parallel client, the client's camera gets disabled (that's exactly the opposite of what i want), leaving only the server's camera enabled.
In other threads and forum topics i read that is not very suitable to try to run all the players with a unique prefab, so now i want each player to have a unique prefab (and hopefully make the work a bit easier), but i don't know how to accomplish this...
The maximum amount of players in my game would be 4, so i think i must differenciate them along all the sessions in a way or another...maybe setting a unique networkView ID for each player? how can i set an ID? and then...enable/disable objetcs or scripts according to IDs?
I'm short of ideas, i'm starting to hate multiplayer programming, the only thing i got is to open and connect to a server :P
Well, thanks in advance!
This would be a good tutorial to learn more about the basics of unity networking:
http://cgcookie.com/unity/2011/12/20/introduction-to-networking-in-unity/
It covers the issue you are having with having multiple player prefabs that are all getting controlled by one player. You seem to be on the right track with networkView.is$$anonymous$$ine. There is no turnkey solution to this, and it requires a decent understanding of how unity networking works to know where to use networkView.is$$anonymous$$ine.
To start, in the player control script, wrap the block of code that takes the user input to move the character with the following if statement:
if (!networkView.is$$anonymous$$ine) {
// $$anonymous$$ovement code
}
This makes it so players only move their own characters.
You might also have camera conflicts. To get around that you could use the same if statement as before in the awake function to disable the camera component on a player prefab if it does not belong to the player.
The more complex your characters are, the more places you will have to use that if statment to turn things off.
Your answer
Follow this Question
Related Questions
Should you disable NetworkView when offline 1 Answer
Camera attached to the wrong player 1 Answer
How to manually handle multiplayer prefabs spawning with our own script ? 0 Answers
How do I update player tilt in multiplayer? 0 Answers
How to update NetworkView on child transform when only the parent is moving? 1 Answer