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 SomeGuy22 · Aug 10, 2012 at 03:28 PM · networkingmultiplayerpositionnetworkviewonserializenetworkview

Strange Variables in OnSerializeNetworkView?

The data I'm receiving is not the data I should be receiving using OnSerializeNetworkView. Is there anything wrong with this code or anything that could end up in weird variables?

 function OnSerializeNetworkView (stream : BitStream, msg : NetworkMessageInfo)
 {
  
  var pos = transform.position;
  var rot = transform.rotation;
  
  var newPos = pos;
  var newRot = rot;
  
  if (stream.isWriting) {
         
         
         if (isactive)
         {
          pos = transform.position;
          rot = transform.rotation;
          newPos = pos;
          newRot = rot;
          Debug.Log("isactive: " + newPos);
          stream.Serialize(newPos);
          stream.Serialize(newRot);
         }
         
         
     } else {
      
        
         
         if (!isactive)
         {
      stream.Serialize(newPos);
          stream.Serialize(newRot);
          Debug.Log("Recieve " + newPos);
          pos = newPos;
          rot = newRot;
         }
         
         
         
     }
     
     if (!isactive)
     {
      transform.position = pos;
      transform.rotation = rot;
      Debug.Log("Transform equals: " + pos);
     }
     
 }

Thanks!

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
6
Best Answer

Answer by Bunny83 · Aug 21, 2012 at 04:19 PM

I've read most of the comments here and i would like to add some general hints:

  • When using the network, make sure your application runs in background. Otherwise the server (or client) won't respond to any package sent when the application doesn't has the focus. That's the most common mistake. You can also set this via scripting at runtime.

  • "Automatic syncing", either by setting a Transform as observed object, or by using OnSerializeNetworkView, only the object owner can send any data and all others can just receive the updates. Owner referes to the NetworkViewID owner. This specifies who owns the object with that ViewID. The owner can't be specified manually. Whenever someone creates a new ViewID, this player will be the owner. Network.Instantiate creates a ViewID for all NetworkViews that are within the object and it takes care of sending them to all others so they are set properly on all peers. So the player that calls Network.Instantiate will own the object and he's the only one who can move the object.

  • When using syncing with an observed Transform, Unity will only send an update when the position / rotation has changed. When using OnSerializeNetworkView you can send any arbitrary data, so deltaCompression will probably be disabled.

  • The SendRate (via scripting) specifies the update interval how often a package is send and therefore how often OnSerializeNetworkView will be called.

  • If you want to disable syncing of a certain NetworkView, just set the stateSynchronization property to Off. In this case, no updates are send.

It's vital that all receiving peers deserialize exactly the same data that you send. You change the data you're sending depending on your isactive variable. When this variable is changing on the sending peer, the receiving peer doesn't know about that.

One way is to send the isactive first and decide on that what and if additional data will follow.

 function OnSerializeNetworkView (stream : BitStream, msg : NetworkMessageInfo)
 {
     var pos = transform.position;
     var rot = transform.rotation;
     var active = isactive;
     
     stream.Serialize(active);  // always send / receive the active flag
     if (active)
     {
          stream.Serialize(pos); // only send / receive pos / rot when active
          stream.Serialize(rot);
     }
     
     if (active && stream.isReading)
     {
         transform.position = pos;
         transform.rotation = rot;
         //isactive = active;  // if you need it also on the other peers.
     }
 }


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 SomeGuy22 · Aug 21, 2012 at 05:08 PM 0
Share

THAN$$anonymous$$ YOU THAN$$anonymous$$ YOU THAN$$anonymous$$ YOU THAN$$anonymous$$ YOU!!!

If I could vote 50 thumbs up I would!!! THAN$$anonymous$$ YOU SIR, YOU SAVED THIS GA$$anonymous$$E FRO$$anonymous$$ COLLAPSE! :D

avatar image Bunny83 · Aug 21, 2012 at 05:34 PM 0
Share

Did I? o.O Well, i'm glad i could help ;)

avatar image ScroodgeM · Aug 21, 2012 at 09:45 PM 0
Share

@Bunny83, +1

just interested in what was the exactly error that i missed in this issue 8)

avatar image SomeGuy22 · Aug 22, 2012 at 01:05 AM 0
Share

Just the code above: I had to always send the position if active, not just if (stream.isWriting), but only apply it if stream.isReading. I think the position was being reset because of an inconsistency in sending/receiving, but I'm not exactly sure.

avatar image
0

Answer by ScroodgeM · Aug 10, 2012 at 08:57 PM

if this happens on two different clients connected to server, then pay attention what happened on server. server in this case is not just resends data, it applies to instantiated object, then reads it again from object and sends to other players. so if you have some differences in scene that could change position of object - that's why your issue happens.

Comment
Add comment · Show 17 · 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 SomeGuy22 · Aug 11, 2012 at 12:48 AM 0
Share

There should be no differences. And I should only be sending data from 1 client, and receiving on the other. And I removed all movement if the client is not sending.

avatar image ScroodgeM · Aug 11, 2012 at 07:51 AM 0
Share

do you use server between clients? or just one of these clients is server? if server between, just check what server receives and what it sends to other clients. methinks you will be surprised 8)

avatar image SomeGuy22 · Aug 11, 2012 at 04:23 PM 0
Share

... I've already done that -- I'm sending the correct position through but the server is receiving something different. There are no differences in the scene and the movement was removed from all except 1 client.

avatar image ScroodgeM · Aug 13, 2012 at 10:06 PM 0
Share

i'm 99.9999% sure that you can't send from client something that can be received by server different. this is only network transfer that can't change data. somewhere (rather on server) data is changed. did you check this data directly in OnNetworkSerializartion method? and on client too?

avatar image SomeGuy22 · Aug 14, 2012 at 03:43 PM 0
Share

That's really strange... When I run the server on the Editor it works perfectly fine, but when I run it from the built client it starts sending and receiving the wrong data. What could be causing this?

Show more comments

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

Unity networking tutorial? 6 Answers

how can i set multiplayer online in my ludo game as i have many objects for a player like dice and 4 players for each player but i can only add one player . What i make my player 0 Answers

Synchronize network view for transform children 1 Answer

Multiplayer - networking without network view? 1 Answer

Smoothing Network Movement with OnSerializeNetworkView 0 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