Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 DanielDollerup · May 09, 2016 at 10:20 AM · multiplayerpositionsynchronization

[UNET] Clients position not being synced after being replaced with ReplacePlayerForConnection

Hello Community!

I've recently started on a simple multiplayer project to learn the ropes of UNET. I got a general idea of how everything works now, but I've stumpled upon a problem when trying to use NetworkServer.ReplacePlayerForConnection.

Thus far, I've got a NetworkManager that spawns a PlayerController (A lobby'ish kinda thing) that controls which team the player should join and setting the player name:

     public override void OnStartLocalPlayer()
     {
         if (!isServer) NetworkServer.Listen(NetworkServer.listenPort);
 
         base.OnStartLocalPlayer();
 
         gameManager = GameObject.Find("_GameManager").GetComponent<GameManager>();
 
         SetUI("Prematch");
 
         CmdSetPlayerName(gameObject, "Player_" + netId.Value.ToString());
     }

After joining the server, the playerName is being set through the server and then synchronized via SyncVar.

Now, the player has but choice and that is to join the game through a button. On click, this runs a method that sets the team and tells the server to create the player object with the NetworkServer.ReplacePlayerForConnection:

 void Btn_JoinGame()
     {
         if (gameManager.team1Players.Count <= 0)
         {
             CmdSetTeam(playerName + "_Controller", Team.One);
         }
         else
         {
             if (gameManager.team1Players.Count <= gameManager.team2Players.Count)
             {
                 CmdSetTeam(playerName + "_Controller", Team.One);
             }
             else
             {
                 CmdSetTeam(playerName + "_Controller", Team.Two);
             }
         }
 
         CmdAddPlayer(playerName + "_Controller");
 
         SetUI("Game");
     }

Now, the CmdAddPlayer does the following:

     [Command]
     void CmdAddPlayer(string target)
     {
         PlayerController pCtrl = GameObject.Find(target).GetComponent<PlayerController>();
 
         GameObject p = Instantiate(playerPrefab);
         p.GetComponent<Player>().PlayerName = pCtrl.playerName;
         p.GetComponent<Player>().Team = pCtrl.team;
 
         NetworkServer.ReplacePlayerForConnection(pCtrl.connectionToClient, p, 0);
         NetworkServer.Destroy(pCtrl.gameObject);
     }

It all works quite well actually, the server instantiates the player on both the client and the server, and both players can move around and send messages to each other, but the clients position on the server is now not getting synchronized. I'm using the Transform Sync thingy that comes with UNET.

Any help is greatly appreciated!

Comment
Add comment · Show 8
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 meat5000 ♦ · May 09, 2016 at 10:37 AM 0
Share

Flagging your client as Ready?

avatar image DanielDollerup meat5000 ♦ · May 09, 2016 at 10:37 AM 0
Share

Hmm, I don't think so. I'll try it.

avatar image DanielDollerup meat5000 ♦ · May 09, 2016 at 10:48 AM 0
Share

That didn't do it, apparently the client object was set to Ready. I even tried forcing it.

avatar image meat5000 ♦ DanielDollerup · May 09, 2016 at 12:25 PM 0
Share

Are your player setup scripts properly Added and actually running when the Object is added?

Place some Debug.Logs in Start() etc to see if its doing what it should.

Oh yes... and you are appropriately retagging/layering your object?

Show more comments
avatar image DanielDollerup · May 10, 2016 at 09:56 AM 0
Share

So, I got this far. Apparently, it works SO$$anonymous$$ETI$$anonymous$$ES - for god knows what reason. I've experimented with the NetworkTransform.Init(), placing it different places with different results. Any help? :o

avatar image DanielDollerup · May 12, 2016 at 09:19 PM 0
Share

So, thus far I have figured out that the client does not send his position (despite being ready, got authority, is a client, got local player authority), which is why the server cannot see the clients move. But I have yet to figure out why the position is not being sent. I'm really reaching here guys

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by DanielDollerup · May 14, 2016 at 07:32 PM

Finally, I found an answer. By placing a ClientScene.SetLocalPlayer I got it to work.

This was my final code:

  [Command]
  void CmdAddPlayer(string target)
  {
     PlayerController pCtrl = GameObject.Find(target).GetComponent<PlayerController>();
     NetworkIdentity pNID = pCtrl.GetComponent<NetworkIdentity>();
     NetworkConnection conn = pNID.connectionToClient;

     GameObject p = Instantiate(playerPrefab);
     p.GetComponent<Player>().Team = team;
     p.GetComponent<Player>().PlayerName = playerName;

     ClientScene.SetLocalObject(pNID.netId, p);

     NetworkServer.ReplacePlayerForConnection(conn, p, 0);
  }

I really hope someone this will help someone! :)

Comment
Add comment · 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

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

70 People are following this question.

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

Related Questions

Photon Syncing Enemies with Other Clients? 1 Answer

edit object only locally 0 Answers

How to Synchronize Play Services Real-Time Multiplayer 0 Answers

getting a GameObject array of all the connected players 1 Answer

Network rotation is laggy and slow 2 Answers


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