Kanda Players 0.7.0
|
We use Unity's Input System to handle player input across different devices. This page explains how input is collected and processed, and how to implement basic locomotion for PC and XR players.
The "Input Collector" approach is used to gather and process input data. This abstraction layer separates the input collection logic from the systems that use the input, allowing for better modularity and easier testing.
Input collectors are implemented as managed components, which allows them to interact directly with Unity's Input System while still being accessible within the ECS framework. They serve as a bridge between the Unity Input System and our ECS-based logic.
Key input collectors include:
IKeyboardInputCollector
: Collects keyboard input for movement and actions.IMouseInputCollector
: Collects mouse input for camera control and interactions.IOpenXrInputCollector
: Collects input data from OpenXR devices.Each collector is responsible for:
For example, the OpenXrInputCollector
sets up actions for HMD position and rotation, and provides methods like GetHmdPosition()
and GetHmdRotation()
to access this data.
To add input capabilities to a player:
KeyboardInputAuthoring
(for keyboard input)MouseInputAuthoring
(for mouse input)OpenXrInputAuthoring
(for OpenXR input)KeyboardInputData
(storing newest keyboard input)MouseInputData
(storing newest mouse input)OpenXrInputData
(storing newest OpenXR input)The InputUpdateSystem
is responsible for updating the Unity Input System.
The system ensures that the Input System is updated manually, which is necessary when using InputSystem.settings.updateMode = InputSettings.UpdateMode.ProcessEventsManually
. This setting is configured in the Startup
class to ensure consistent behavior across all platforms.
To implement basic movement and looking for PC players:
PcHeadKeyboardMovementAuthoring
and PcHeadMouseRotationAuthoring
to your player head prefab.PcHeadKeyboardMovement
and PcHeadMouseRotation
.PcHeadMovementClientSystem
: Handles keyboard-based movementPcHeadLookClientSystem
: Handles mouse-based lookingNote: PcHeadMovementClientSystem
and PcHeadLookClientSystem
depend on PcPlayerSettings
, which is produced by PlayerSettingsSystem
. PlayerSettingsSystem
constructs PcPlayerSettings
by combining default configuration values with any user-specific preferences.
To implement basic movement for VR players:
OpenXrHeadMovementAuthoring
to your XR player head prefab.OpenXrHeadMovement
.OpenXrHeadMovementSystem
will use this component along with the OpenXrInputData
to move and rotate the VR player's head based on the HMD's position and rotation.For multiplayer functionality, input needs to be synchronized between the local player and its remote representation. This is achieved through a combination of components and systems:
ClientTransformInput
: An IInputComponentData
that allows a client to control the transform of a ghost by providing a target transform as input.LocalPlayerPartRef
: Keeps a reference to a local player part, used on remote player parts to replicate data from the local player.SynchronizePlayerOriginClientSystem
:SynchronizePlayerHeadClientSystem
:PlayerHead
, ensures it has a reference to the local PlayerHead
.LocalTransform
of the local head to the ClientTransformInput
component of the remote head.ClientTransformInputSystem
:ClientTransformInput
to the adjacent LocalTransform
.This synchronization setup ensures that:
To add new types of input:
Startup.cs
This structure allows you to extend the input system to support new devices or input methods while maintaining compatibility with the existing ECS framework.
We provide ECS-based mouse cursor control through the CursorState
component and CursorStateInitializationSystem
.
1. Add CursorState
component to an entity:
This is added by default if using the high-level LocalPcPlayerHeadAuthoring
script.
2. The CursorStateInitializationSystem
will apply these settings on startup.
Query and modify cursor state from any system:
Available lock modes:
None
: Cursor moves freelyLocked
: Cursor is hidden and locked to centerConfined
: Cursor stays within window bounds