Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
277 views
in Technique[技术] by (71.8m points)

.net - Unity custom Server-Client UDP

I am working on a school project where we are making a game in unity and its supposed to be multiplayer. I am not allowed to use any inbuilt server/client libraries like UNET or MIRROR because its a networking course not game dev.

So far I've got a working UDP .NET server that can receive and send data. Whenever a client connects it stores the info (IPEndPoint) so that it can be used in the future. Whenever the server receives a packet from a client, the server sends the packet to every other client.

Thank you for reading this far. Should the Server tell the clients which ClientID they have? I guess the packet has to be marked with some kind of ID to identify which client sent it. Also in Unity, How would i go about spawning and "moving other clients gameobjects" when i recieve data that they have changed position. Ive seen that the NetworkManager and UNET uses IsLocalPlayer to identify if its the local player, i cant find the underlying code but i guess its just a bool you set when instantiating the object?

So what i've got right now is a game, the client can connect to server, the client can send its transform.position to the server, the server prints it out and sends it to other clients.

All help and ideas is appreciated, and if this is a stupid question please tell me where to read more because all tutorial i see use network libraries and thats what i would do if i was allowed to.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You really face a long journey!

Should the Server tell the clients which ClientID they have?

On the server side you should have these abilities:

  1. Send to everyone connected

  2. Send only to some specific connection

If you don't have that sorted out, you'll have trouble.

Should the Server tell the clients which ClientID they have?

It's your choice. The server can "choose all the IDs" or the clients can "choose their own id".

A simple approach is: when a client is launched, just create a UUID and use that. So, in this example the client is choosing their own ID. The first communication to the server is something like "hi, my uuid is2i23u4y2i34u2y4ui and my user's name is Jane Jones". On the server side, keep track of all the uuids and connections and nicknames, etc etc.

(If you're not already familiar with math concepts like UUIDs, you face a long journey.)

As I say, it's your choice. The server can "choose all the IDs" or the clients can "choose their own id".

"How would i go about spawning and moving other clients gameobjects etc

Well in clientA you'd have a function called something like "SpawnARobot(position)"

When commands arrive at clientA, you'll parse the commands. You'll have a big switch statement to know how to handle various commands. One of your commands might be "spawn" and when clientA receives a "spawn" command clientA would call "SpawnARobot(position)"

Here's literally an example of some commands (ie, just some text) arriving, and then the relevant functions/whatever being called:

enter image description here

Note that you have some really basic concepts to work out.

In some games only the server "decides" that something is spawned, or, whatever. The clients are completely dumb and essentially just "display what they are told".

In other games the server doesn't really do anything other than pass around commands, and, the clients themselves "decide" that something should be spawned, or, whatever. (That is often an "rpc" approach.)

Also don't forget there is a HUGE basic confusion between ... say you have three iPhones playing your Game. We speak of a "server" (call it a "master controller") which is one of those three iPhones, so each GameOnAnIPhone has your "client" but one (only) is also the "server" (or "master controller"). In complete contrast to that you can have literally "a server" in the cloud, ie your code (very likely Node.js) running on an AWS instance, and all three GameOnAnIPhones connect to the "server server".

All of these concepts are incredibly different, you have to sort out these basics I'm afraid.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...