Skip to content

Instantly share code, notes, and snippets.

@ChristianTucker
Last active November 25, 2021 10:46
Show Gist options
  • Save ChristianTucker/d5ec85a59f449871742d1668734d0807 to your computer and use it in GitHub Desktop.
Save ChristianTucker/d5ec85a59f449871742d1668734d0807 to your computer and use it in GitHub Desktop.
public class CharacterController : MonoBehaviour
{
private ClientInputState inputState;
private Vector3 velocity = Vector3.zero;
private int simulationFrame;
private void Start()
{
NetworkClient.RegisterHandler<SimulationState>(OnServerSimulationStateReceived);
}
private void Update()
{
// Only run this code on the client.
if(NetworkServer.isActive) return;
inputState = new ClientInputState
{
keysPressed = Keybinds.PressedThisFrame(),
keysDown = Keybinds.BeindHeldDown(),
keysReleased = Keybinds.ReleasedThisFrame()
};
}
private void FixedUpdate()
{
// Only run this code on the client.
if(NetworkServer.isActive) return;
// Set the inputState's simulation frame.
// NOTE: We don't do this in update, as FixedUpdate is called
// in conjunction with the physics time step, and not once per frame.
inputState.simulationFrame = simulationFrame;
// Increase the client's simulation frame.
++simulationFrame;
}
private void OnServerSimulationStateReceived(NetworkConnection connection, SimulationState message)
{
// TODO: process the server's simulation state, comparing it against the
// client's cached state for the provided simulationFrame.
}
}
@ChristianTucker
Copy link
Author

@hellostorman I'm not really sure what your question is, this was intended just to be Psuedo. In this example Keybinds is intended to be used everywhere, and Keybind has no use and should be refactored to Keybinds. You should create a collection that keeps track of which keys are pressed and when they were pressed so you can do lookups on key combinations as well as how long keys have been held down. You should look into bitmasks and enums to properly determine how to return the bound keys.

For example..

public enum Keybind {
  NONE = 0,
  MOVE_LEFT = 1,
  MOVE_RIGHT = 2,
  JUMP = 4,
  MOVE_UP = 8,
  MOVE_DOWN = 16
}

and then you can declare these such as

public byte keybinds = Keybind.None;

You can use bitwise operators to add or remove data from the byte (or short/int/long depending on the amount of flags you use)

@michaels-account
Copy link

I wasn't 100% sure how to ask my question since there was a lot I didn't understand, but thanks for taking the time for that reply, I'll take a look into it!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment