Unity Fighting Game with 2 players

Post here your questions about SFS2X. Here we discuss all server-side matters. For client API questions see the dedicated forums.

Moderators: Lapo, Bax

Aymeric
Posts: 2
Joined: 17 Feb 2017, 14:25

Unity Fighting Game with 2 players

Postby Aymeric » 17 Feb 2017, 14:50

Hi there,

So I read a lot of documentation and go through the tutorials. I'm working on a fighting 2D game (like Street Fighter) with 2 players only made with Unity. Unfortunately I didn't find enough informations about this, the topics on the forum were very brief.

I'm using SmartFoxServer for Room creation (2 players max) and then the fighting part though it could be done via P2P or Socket (but I don't want to bother with Firewall issue etc.).

I want to benefit from Unity engine and its physics, so the Room creator will be the Master and the other one the Slave. Since there are only 2 players, I don't need to run the physics on the server, right? It will save me lot of time because it will be easier with Unity.

This is my current process:
- the Master client run physics on its side, and send users positions to the Server.
- the Server dispatches this information to the other player.
- the Slave receives both user positions and make an interpolation.
- the Slave sends its input to the server.
- the Server dispatches the input to the Master.
- The Master applies input on the remote player and so moves its physics. Then the current process starts again.

Obviously I sent only informations when something is changing, so if a player doesn't move there isn't any message sent to the Server.

As you can imagine, I suffer from lag... What I think to do next :
- use UDP.
- use RoomExtension instead of ZoneExtension, does it will change anything concerning performances?
- I send messages in a Unity's update method (so 60 fps), move to a 10 fps max method.
- put physics too on Slave side so it can also move on its side directly after the input and then interpolate from the Master's result.

What do you think? Am I missing something? With my architecture, the slave must send informations to server then master then go back to server then slave, is it a big mistake or not? If it's, I will have to change all my fighting game logic on the server and it will take way more time to program.

Thanks for your help,
Aymeric
User avatar
Lapo
Site Admin
Posts: 18801
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Unity Fighting Game with 2 players

Postby Lapo » 17 Feb 2017, 15:27

Hi,
yes this will work. Running physics on the client makes sense. From a security standpoint there's always a risk that somebody will hack the client and use such modded app to put the other player at a disadvantage. This however is an issue basically with any Unity multiplayer game, since they are all P2P, basically. There are ways to defend against this, but let's not go off topic.

- use UDP.

For some messages yes, such as position changes, but not for everything, since UDP messages can get lost. TCP on the other hand attempts retransmissions so packets will get there, eventually.

- use RoomExtension instead of ZoneExtension, does it will change anything concerning performances?

No, not really.

- I send messages in a Unity's update method (so 60 fps), move to a 10 fps max method.

This is more of a problem. If you send packets at 60hz you will leave out a lot of people without a blazing fast connection. It would be best to optimize the game for 20-30pps (packets per second) for TCP and with UDP you can experiment with pushing it a bit higher.

put physics too on Slave side so it can also move on its side directly after the input and then interpolate from the Master's result.

This is more an area of experimentation. It certainly makes sense as it will help alleviate input lag for the slave. Maybe only the slave will be able to register a hit, though.

Cheers
Lapo
--
gotoAndPlay()
...addicted to flash games
Aymeric
Posts: 2
Joined: 17 Feb 2017, 14:25

Re: Unity Fighting Game with 2 players

Postby Aymeric » 20 Feb 2017, 15:26

Thanks for the detailed answer Lapo. So I moved to UDP for transform update, and reduce packets frequency that way (in my Update loop);

Code: Select all

if (masterTimeLastSending >= SmartFoxConnection.sendingPeriod) {

   SmartFoxGame.Instance.SendLocalPlayerPhysicsPosition();
   localPlayer.transform.hasChanged = false;

   masterTimeLastSending = 0;
   return;
}
masterTimeLastSending += Time.deltaTime;


My ping has reduced, this is a really good thing. But the synch is far to be good. For the interpolation, I'm trying to use the NetworkTransformInterpolation class from the FPS example. So my Master sent its date with the position, at the opposite of the samples (where it comes from the Java Server).

Return to “SFS2X Questions”

Who is online

Users browsing this forum: No registered users and 3 guests