简体   繁体   中英

C#/Unity - TCP/UDP Multyplayer server for Positions and Rotations update?

I am using Unity 2018.3.14 and I have created my self TCP and UDP server. I use the TCP layer to send not that much intense and repetitive messages like simple chat or damage done.

I use the UDP connection to send messages like player position and rotation.

Basically when 1 player moves I send all the coordinates that Unity sees for his movement (x,y,z) he made from point A to point B to all other connected players.

This causes a lot of messages to be sent. Let's imagine I have 100 players connected and all of them running at the same time. I believe this will flood the server.

So my question is it a good idea to transmit all the coordinates (x,y,z) the player reached in his journey from point A to point B or you can suggest better approach?

This can be a really big article and having trouble with it myself I'll share my knowledge with you. This may be vary depending of your game.

1) Physics Sync with Deltas

Instead of sending the position again and again to everyone you can send the speed changes that happens. For example you press the key "W" and move forward, instead of constantly send the position, you send a message that represent that you are moving, something like "My Velocity is Vector3(0,0,10)" each client that will receive this message will start moving the said object. When you release the "W" key it will send a new message "My Velocity is Vector3(0,0,0)" so I'm basically stopped.

You will have to sync every now and then your position just to make sure that everyone is in correct place but nothing like flooding with position packets.

2)Relative Objects

In a game with big map or strange rooms and geography you can update ONLY the clients that:

  • You can see them
  • They are within a proximity of X radius from you
  • Might interact with you with any way

Example you don't need to send your position to someone who is so far away that it might not even know your existence.

This can heavily reduce your traffic especially if you have a lot of players spread over a big area.

3)Packet Messaging

Based on your implementation you will have to send X number of messages per second. Either this is Position updates, Velocity Updates or whatever you decide to send. Keep each packet to a minimum. Examples:

  • Dont send "Player A moved from Point1 to Point2", send "Player A is now at Point2" and have the client Interpolate its new position.

There can be a lot of optimizations at this point based on your approach.

Based on your game some of these might not work for you.

Usefull Links:

Snapshot Compression

GameDev Usefull Post

For more information feel free to contact me.

For any networking games you should define the send rate of the position / rotation. This is not recommended to send at the same rate of Update() (60 per seconds). You have to create a movement script that smooth the difference between your Update() rate and your networking position/rotation sending rate.

Even in the deprecated UNET there is the option: Network Send Rate (seconds)

I hope I have been clear enough about this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM