Remote Controls
Depending on the approach you decide to take on your game, you can send object data from clients but send client input controls only.
Question : But was it considered a Control?
Answer : Control is anything that the system can use to interact with the game, such as Keyboard, GamePad, Joystick, etc...
Remote controls can be synchronized in two ways, by using NetworkManager or in case of script inherits from NetworkBehaviour by code.
Synchronized using NetworkManager
When Remote Controls is enabled, a system with put all objects as passive ( event local player ) and your player will move only based on server position and actions.
This can be very useful if you are trying to isolate your game from any cheating or hacking, since the server will only process player inputs, instead of sending and receiving his position and events to the server.
To use Remote Controls you will need to take the following steps :
- Create an script o implement how you with catch Inputs
This script must implement IInputProvider interface.
Each method of this script must return the state of input witch he is mapping, the following code is an example of how to implement those methods.
public bool IsJumpPressed() {
return Input.GetKey(KeyCode.Space);
}
public bool IsFrontPressed() {
return Input.GetKey(KeyCode.UpArrow);
}
public bool IsBackPressed() {
return Input.GetKey(KeyCode.DownArrow);
}
public bool IsLeftPressed() {
return Input.GetKey(KeyCode.LeftArrow);
}
public bool IsRightPressed() {
return Input.GetKey(KeyCode.RightArrow);
}
public Vector2 GetMovement() {
return new Vector2(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"));
}
Each method maps one input, and the possible return values shall be only bool, float and Vector2.
You can also check the example script Input Provider in the examples folder. This script is a full implementation of the Input Provider.
- Assign prefab and component where script provider is attached
- Register all possible controls on NetworkManager
Each method must have a unique alias to be identified by the system and sent through the network.
- Get an instance of ObjectNet class "NetworkObject" from current NetworkPrefab.
this.network = this.GetComponent<NetworkObject>();
- Instead of using the direct Unity Input Methods ( or any other API ) get input status, you need to use ObjectNet input methods.
this.network.GetInput<bool>("MoveLeft");
this.network.GetInput<bool>("MoveRight");
this.network.GetInput<bool>("Jump");
Synchronized using by code NetworkManager
Remote control can also be synchronized by code, this can be useful if you need to do some rebinding or procedural generation. The following piece of code shows how to synchronize Network Inputs by code.
public void Awake() {
// To map controls
this.RegisterInput<bool>("jump").OnEvaluate(() => { return Input.GetKeyDown("space"); });
this.RegisterInput<float>("lados").OnEvaluate(() => { return Input.GetAxis("horizontal"); });
}
Each control needs to be mapped with the method which will return his current status.