LocalInputController - jimdroberts/FishMMO GitHub Wiki
The LocalInputController
is a MonoBehaviour responsible for handling local player input in FishMMO. It manages input for character movement, camera control, UI toggling, and interaction, integrating with the InputManager
and various character and UI interfaces. This controller ensures that input is processed only when appropriate (e.g., when the character is alive and UI does not have focus) and translates input states into movement and camera actions for the player character.
-
private const string MouseXInput, MouseYInput, MouseScrollInput, HorizontalInput, VerticalInput, JumpInput, CrouchInput, RunInput, ToggleFirstPersonInput
String constants for input axis and action names used throughout the controller.
-
private bool _jumpQueued
Indicates if a jump input has been queued for processing.
-
private bool _crouchInputActive
Indicates if crouch input is currently active.
-
private bool _sprintInputActive
Indicates if sprint input is currently active.
-
public IPlayerCharacter Character { get; private set; }
The player character associated with this input controller.
-
public void Initialize(IPlayerCharacter character)
Initializes the input controller for the specified player character and subscribes to input handling.
-
public void Deinitialize()
Deinitializes the input controller, unsubscribing from character input handling.
-
private void OnEnable()
Unity event called when the object becomes enabled; shows key UI elements for the player.
-
private void OnDisable()
Unity event called when the object becomes disabled; hides key UI elements for the player.
-
private bool CanUpdateInput()
Determines if input should be processed for the player character (e.g., only if alive and mouse mode is off).
-
public KCCInputReplicateData KCCPlayer_OnHandleCharacterInput()
Handles character input for KinematicCharacterController, converting input states into movement replication data.
-
private void Update()
Unity event called every frame; updates input states.
-
private void UpdateInput()
Handles UI and gameplay input, including movement, interaction, and UI toggling.
-
private void LateUpdate()
Unity event called every frame after all Update functions; handles camera input for the player character.
-
private void HandleCameraInput()
Processes camera input, including rotation and zoom, based on player input and physics movers.
- Attach
LocalInputController
to the player character GameObject. - Call
Initialize(IPlayerCharacter character)
to set up input handling for the character. - Ensure the
InputManager
and UI systems are properly configured. - The controller will automatically handle input, UI toggling, and camera control during gameplay.
// In your player setup code:
localInputController.Initialize(playerCharacter);
// To clean up:
localInputController.Deinitialize();
- Always check if the character is alive and UI does not have focus before processing input.
- Use the provided constants for input names to avoid typos and ensure consistency.
- Integrate with the
InputManager
for flexible key and axis rebinding. - Use
OnEnable
andOnDisable
to manage UI visibility for a seamless player experience. - Clean up event subscriptions in
Deinitialize
to prevent memory leaks or unwanted input handling.