We're developing a 2D PvP online action game. You can think of it like Super Smash Bros. We have a function call tree for characters like below:
CharacterInput.Update() |-- CharacterController.StartJump() |-- CharacterMovement.TranslateState( "Jump" ) |-- CharacterJumpState.OnEnter() |-- CustomRigidbody2D.Move( delta )
I think the pseudo-code above is kinda straight forward. It's a typical
Movement structure where the
Movement part is made using several character states. Also, the
Movement part is responsible for animations, physics, visual effects and sound effects.
So, to make this networked, we need to replicate some values over the network.
To make it clear, we're using Photon Bolt for Unity, which provide a
ExecuteCommand way to deal with server authority and client prediction. You can check it out here.
So in terms of replication, we realized that according to the structure, we should decide which part to replicate. According to the call tree above, we can split our character logic into these layers:
- Input Layer: LeftStick, RightStick, A, B, X, Y, ...
- Controller Layer: MovementDirection, ShootDirection, IsJump, IsShoot, IsMelee, ...
- Movement Layer: CurrentState and all of its member variables
- Rigidbody, Animation Layer: Velocity, GravityScale, CurrentAnimationState
Theoretically, replicate any of these layers can make it work because the process of higher layer to lower layer is deterministic. Once the higher layer is replicated, lower layers should be the same across the network.
Sadly, this is just theory. In reality, you must deal with lag, and unreliable packet delivery.
For example, if you only replicate the inputs, an unstable network may make it out of sync because state A may transit itself to state B if animation ends, but actually it should be transitted to state C if player presses some button.
However, if you only replicate rigidbody and animation, all the visual effect or audio effect triggered by CharacterState can't work, because
doesn't even know which state it is in...
So, my question is, how do other games replicate their character? Especially in a PvP game?