PlayerInputManager
public class PlayerInputManager : MonoBehaviour
{
public static PlayerInputManager playerInputManager;
public static void SetPlayerEntity(NetworkEntity p)
{
if (playerInputManager != null &&
(playerInputManager.playerEntity == null || playerInputManager.playerEntity.entityID != p.entityID))
{
try
{
PlayerNetworkEntity pe = (PlayerNetworkEntity)p;
if (pe != null)
pe.ghostPos = playerInputManager.ghostObject;
}
catch(Exception e)
{
Debug.LogError("Not a player network entity :O");
}
playerInputManager.playerEntity = p;
}
}
public static float deadzone = 0.5f;
public List<PlayerInput> playerInputs = new List<PlayerInput>();
public int maxPlayerInputs = 300;
//inputs sent from clients sent to a server
//public List<PlayerInput> recievedInputs = new List<PlayerInput>();
//which networkEntity do I control? Not set within this class, let it be the
//player objects problem
public NetworkEntity playerEntity;
public GameObject ghostObject;
public HPui hpText;
PlayerInput newInput;
PlayerInput previousInput;
public GameObject cameraObject;
//...
This class picks up user input (currently the Unity default input manager approach, recommend dropping for Input System) and has methods to action those inputs on the current set playerEntity at the right times. There are other references to the camera and ui elements to show the users hp. Recommended you heavily change this class for your own needs.
Methods
public static void SetPlayerEntity(NetworkEntity p)
This sets which PlayerNetworkEntity we should currently control, so once you know which NetworkEntity you control, you should call this method to get user input to start working. Server players know right away who they are, but clients usually need to wait till some packets come in about the gameplay before setting which one they control.
private void Start()
Just calls playerInputManager = this
which is static, so we can lazy singleton access this class from anywhere
private void Update()
Collects user input and stores it against newInput, also updates the camera and ui for a smoother experience. DO NOT ACTION your user input here, that has to have happen at the right phase during network entity management updates
public void ProcessLocalPlayersInputs()
Method to action the user input on the playerEntity we are controlling. See where this method is being called within the project to see where to action this
public InputState GetPreviousButtonState(string btn)
Pass it a button name e.g “A” and it will return its previous input state or InputState.nothing if the button does not exist. Currently setup with buttons A,B,C,D but add more if you need to.
public void AddPlayerInputToList(PlayerInput p)
Some sort of info
public void SetButtonPressed(ref InputState button, bool pressed, InputState previousButtonState)
Set a buttons press state (pressed, held, nothing)
public void AddPlayerInputToList(PlayerInput p)
Adds a PlayerInput to the playerInputs list to be referenced at a later time
public void ProcessReceivedInput(PlayerInput input, PlayerConnection player)
After receiving input from a client, we try to action it if we haven’t actioned this playerInputID before
public void ProcessInput(PlayerInput input)
Finds PlayerNetworkEntity this input belongs to and actions it against them
public PlayerInput GetPlayerInputByID(int id)
Returns a playerInput with matching id otherwise returns null
public List<PlayerInput> GetUnprocessedInputsAfterThisID(int id)
Gets all playerInputs with playerID greater then id passed in. Helpful for rollback