- Home /
Help in Multiplayer
Hello everyone, So today I tried multiplayer for the first time (I'm a noob in that by the way), so I used unity's basic multiplayer assets and followed this guys tutorial: https://www.youtube.com/watch?v=qp_rBK-ZD5g Everything works fine except my friend and I share the same controller. How can I make it so everytime someone connects, it spawns a different controller. Thanks.
UPDATE: Here is my error screen :
Answer by Muuskii · Aug 05, 2012 at 06:28 AM
Sounds like the sort of thing you would do in OnPlayerConnected
First, I tried the code above on my first person controller, but it wouldn't work. Then I noticed that the system was telling to make a prefab of my Controller, so I did. But still I (the master of the server) am controller both me and my friend. Also we can't see each other even though I have applied the network view to my Player
If you're using Network.Instantiate to make the controller, whoever calls the function will be the owner of the controller. You will need some way to connect the controller to a server owned "body" if you're making an authoritative system.
I personally use AllocateViewID to first create a ViewID on the client, pass it using an RPC to the server. The server then allocates it's own ID and passes it along with the client ID to a OthersBuffered RPC called "spawn player"
What this does is it sends everyone two things: a client controlled network ID and a server controlled one
All clients that do not control the character will create just a prefab with a networkview set to the Server's ID; probably observing the NetworkRigidbody script.
The server and the controlling client will do the same but add a networkview with the client's ID observing a "controller script"
The issue you'll run into next is the one I had to fix last week; The buffered RPC creating the character belongs to the server, and won't get deleted when the player disconnects.
The fix (for me) was to add a "DestroySelf" RPC to NetworkRigidbody and call it to AllBuffered when a player leaves. You'll need to make a loop that checks all GameObjects if they have a server ID AND a client ID on them, because you can only call the RPC on the server ID. (because the client's ID doesn't exist after he leaves)
This is all fairly more advanced stuff, and it took me quite awhile to figure it all out so it's understandable if you want to go with a more simple non-authorative system.
Now I kind of understand how this works. But I still don't know how to fix my current problem. $$anonymous$$y friend and I still control the same Player and we of course can't see each other.
Ok so I make both AllocateViewID and on OnPlayerConnected and posted them both on my Player. Set the variable "Cube Prefab" an empty object. Now we both controll 2 different players but we still cannot see each other.
Answer by Muuskii · Aug 05, 2012 at 08:08 PM
Alright, try this:
void OnConnectedToServer()
{
GameObject clone = Network.Instantiate (cubePrefab, position, rotation, 0);
clone.AddComponent("PlayerControlScript"); //this script controls movement
//Make your camera follow clone here
}
Make sure the "cubePrefab" only has the physical part of the character (mesh, colliders, etc) and a networkview observing the Transform component. Make sure there are NO cameras in the prefab.
This will give you a non-authorative server game where two players can see eachother.
That is correct, usually the client must grab the Camera.main and set it up to follow the newly made GameObject. I usually do this by having a CameraFollow script with a public target variable.
Assets/Assets/Scripts/Player.cs(1,6): error CS0116: A namespace can only contain types and namespace declarations
Your answer
Follow this Question
Related Questions
Not Connecting to Server 0 Answers
Distribute terrain in zones 3 Answers
Multiplayer Lobby & Gamemode selection Coding 0 Answers
Multiplayer Support for Android 1 Answer
Connection between 2 scripts 1 Answer