Server-Authoritative Player Controller
Summary
Create a game where each player controls a server entity using a player controller. Player controllers are used to control player entities using player inputs in server-authoritative games. The controller runs on both the client and the server, and on the client it's used to predict motion to give the feeling of immediate response. The server has full control over the player's entity to prevent cheating. Player controllers are the recommended way to control entities in highly-competitive games where it is important to prevent cheating, and in games that have physics-interactions between player entities and other entities. The alternative to server authoritative movement with player controllers is client authority, which can be good for less competitive games, such as co-op games played with friends.
Requirements
Completion of Tutorial 1.
Scene and Prefab Setup
Create a Room
Right click in the hierarachy window and create a 'Reactor->Physics Room' game object.
Create a Static Platform to be the Ground
Right click in the hierarachy window and create a cube. Rename it to 'Platform'.
Set the transform position to (0, 0, 0).
Set the transform scale to (20, 1, 20).
Click the 'Add Component' button in the inspector and add a 'Reactor->ksEntityComponent'.
Create Dynamic Cubes on the Platform for the Player to Push Around
Right click in the hierarachy window and create a cube.
Add a ksEntityComponent.
Add a Rigidbody component.
Place the cube on the platform. Copy and paste it to place more dynamic cubes wherever you like. The player will be able to push these around. You can make stacks of dynamic cubes.
Create an Avatar Entity Prefab that the Player will Control
Create a '3D Object->Capsule' in your scene and name it 'Avatar'.
Add a ksEntityComponent.
Add a Rigidbody component.
In the inspector for the Rigidbody, check all of the 'Freeze Rotation' constraint properties. This will prevent the physics simulation from rotating the avatar.
Add a cube as a child of the avatar.
Set the cube scale to (.25, .25, 1) and the position to (0, 0, .25). The cube will stick out in front of the avatar, allowing you to tell what direction it is facing.
Make the avatar a prefab in the 'Resources' folder.
Delete the avatar from the hierarchy.
Scripting
Create a Player Controller Script
Player controllers allow players to control a server entity. They are executed both on the client and the server. On the client they are used to provide input prediction, and on the server they are used to apply player inputs to entities.
Select the '/Assets/ReactorScripts/Common' folder in the project window and right click. Select 'Create->Reactor->Player Controller' to create a player controller and name it 'AvatarController'.
All controllers must be placed under '/Assets/ReactorScripts/Common' or another common folder, thus the context menu will only allow creating player controllers from common folders.
All scripts in common folders are available on both the client and the server, and may not contain any Unity references.
To create a common folder, first create a folder. Then right-click in that folder in the project browser and select 'Create->Assembly Defintion Reference'. In the inspector for the assembly reference, set 'Assembly Defintion' to 'KSScripts-Common'
AvatarController.cs
Create an Avatar Controller Asset
Once you create a player controller class, you can create assets from it and edit their ksEditable fields.
Player controller assets must be in a 'Resources' folder or asset bundle. To use player controller assets from an asset bundle, you must call
ksReactor.RegisterAssetBundlewith the asset bundle.
Right click in a 'Resources' folder and select 'Create->Reactor->AvatarController' and name it 'AvatarController'.
Create an Input Binding Script
Input bindings need to be registered in the Reactor API for the controller to work. We will bind the predefined Unity inputs "Horizontal", "Vertical", and "Jump" to the Axes and Button constants we defined in the AvatarController.
Create a Monobehaviour and name it 'BindInputs'.
Attach it to your 'Room' object.
BindInputs.cs
Instead of using
BindAxisyou could useBindRawAxis.BindRawAxisgets the value using Unity'sInput.GetRawAxiswhich only returns -1, 0, 1 for digital inputs such as keyboard or gamepad buttons, whereasBindAxiswill use Unity's smoothed axis values that change the value over time.
Create a Server Room Script
Our server room script will spawn an avatar for players when they connect and makes them the owner. The avatar will automatically be destroyed when the owner disconnects.
You can prevent an owned entity from being destroyed when its owner disconnects by unchecking the 'Destroy On Owner Disconnect' property in the ksEntityComponent inspector.
Select the 'Room' object.
In the 'Add Component' menu, select 'Reactor->New Server Room Script'.
Name the script 'ServerRoom'.
Edit the script to contain the code below.
Assign the 'AvatarController' asset you created to the
Controllerproperty in the inspector.
ServerRoom.cs
Create a Follow Camera Script
The follow camera script will follow a target game object from a topdown perspective. Later we will set the target to be the local player's avatar.
Create a Monobehaviour named 'FollowCamera' and add it to the 'Main Camera'.
FollowCamera.cs
Create a Server Avatar Script
The server avatar script will spawn a new avatar for a player if they die (from walking off the platform).
Select the 'Avatar' prefab.
In the 'Add Component' menu, select 'Reactor->New Server Entity Script'.
Name the script 'ServerAvatar'.
ServerAvatar.cs
Create a Client Avatar Script
The client avatar script sets the FollowCamera target to be the avatar if the avatar is controlled by the local player, and converts the mouse position to a rotation input.
Select the 'Avatar' prefab.
In the 'Add Component' menu, select 'Reactor->New Client Entity Script'.
Name the script 'ClientAvatar'.
ClientAvatar.cs
Testing
Build your scene config (CTRL+F2).
Start a local server.
Enter play mode.
When you connect, an avatar should spawn at a random location above the platform. You can move around using WASD or the arrow keys. Pressing space will jump. You can keep jumping in mid-air. Your avatar will rotate to look at the mouse. If you fall off the platform, you will respawn above the platform. The avatar will be destroyed when you disconnect.
Last updated

