Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Radon · Aug 05, 2012 at 05:31 AM · c#javascriptmultiplayerconnection

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 : alt text

error.png (73.5 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Muuskii · Aug 05, 2012 at 06:28 AM

Sounds like the sort of thing you would do in OnPlayerConnected

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Radon · Aug 05, 2012 at 07:45 AM 0
Share

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

avatar image Muuskii · Aug 05, 2012 at 02:52 PM 0
Share

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.

avatar image Radon · Aug 05, 2012 at 07:12 PM 0
Share

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.

avatar image Radon · Aug 05, 2012 at 07:22 PM 0
Share

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.

avatar image
0
Wiki

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.

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Radon · Aug 05, 2012 at 09:13 PM 0
Share

So the cube prefab is my character but without the camera?

avatar image Muuskii · Aug 05, 2012 at 09:34 PM 0
Share

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.

avatar image Radon · Aug 05, 2012 at 11:25 PM 0
Share

Assets/Assets/Scripts/Player.cs(1,6): error CS0116: A namespace can only contain types and namespace declarations

avatar image Muuskii · Aug 06, 2012 at 02:44 PM 0
Share

What line is that on?

avatar image Radon · Aug 06, 2012 at 06:17 PM 0
Share

On the player

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

9 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges