-
-
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. | |
} | |
} |
@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)
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!
Hey apologies I know this is a beginner question but I've been stuck on this all day. I'm struggling to implement the ClientInputState.
Your private void update has
inputState = new ClientInputState
{
keysPressed = Keybinds.PressedThisFrame(),
keysDown = Keybinds.BeindHeldDown(),
keysReleased = Keybinds.ReleasedThisFrame()
};
and inside the ProcessInputs method theres
bool moveForward = state.keysDown.Get(Keybind.MOVE_FORWARD);
bool moveBackwards = state.keysDown.Get(Keybind.MOVE_BACKWARDS);
bool moveLeft = state.keysDown.Get(Keybind.MOVE_LEFT);
bool moveRight = state.keysDown.Get(Keybind.MOVE_RIGHT);
I'm unsure if Keybinds and Keybind 2 seperate methods, and the if state.keysDown.Get is instead "Add".
public class Keybinds
{
public bool MOVE_FORWARD = Input.GetKey(KeyCode.W);
public bool MOVE_BACKWARDS = Input.GetKey(KeyCode.S);
public bool MOVE_LEFT = Input.GetKey(KeyCode.A);
public bool MOVE_RIGHT = Input.GetKey(KeyCode.D);
public void PressedThisFrame()
{
}
public void BeindHeldDown()
{
}
public void ReleasedThisFrame()
{
}
}
I've played around with something like this but there's an error saying "An object reference is required for the non-static field, method, or property "Keybinds.MOVE_FORWARD".
Honestly any help would be super appreciated!