We provide systems for handling player interactions that integrates with the Kanda interaction system and supports both VR controllers and mouse input.
Overview
Player interaction systems consist of several key components:
- Player-associated interactors that can be activated by input
- Support for both mouse-based ray interaction and OpenXR controller-based point interaction
- Performance optimization through predictive ghost switching
Setting Up Interactors
Player Interactor Spawner
Interactors are associated with players using the PlayerInteractorSpawner
component. This spawner:
- Creates an interactor instance for the player
- Attaches it to the appropriate player part
- Ensures correct ghost ownership for networking
- Handles cleanup when players leave
public struct PlayerInteractorSpawner : IComponentData
{
public Entity PrefabEntity;
public float3 PositionOffset;
}
Available Interactor Types
We provide pre-configured interactor prefabs in Assets/Prefabs/Interactors
:
- Mouse Ray Interactor: Uses camera-based raycasting for desktop interaction
- OpenXR Controller Interactor: Provides direct interaction for VR controllers
Input Handling
Interactor activation is handled by two primary systems:
PC Input
The PCInteractorInputClientSystem
maps mouse and keyboard input to interaction events:
- Mouse inputs:
- Left mouse button → Primary interaction
- Right mouse button → Secondary interaction
- Keyboard inputs:
- Activate button (E) → Primary interaction
- Q grab button → Secondary interaction
- Space button → Teleport
- Movement buttons
- Panning buttons
- Sprint button
You can find more details about grabbing on PC in the PC Grabbing guide.
OpenXR Input
The OpenXrInteractorInputClientSystem
maps VR controller input:
- Trigger press → Primary interaction
- Grip press → Secondary interaction
Performance Optimization
Prediction Switching
We include an automatic prediction switching system for interactables to optimize performance:
- Configuration:
- Access the Kanda Player settings in Project Settings
- Set the
InteractionPredictionRange
to define the distance from the player where interactions should be predicted
- How it works:
- Interactables within the prediction range use client-side prediction for responsive interaction
- Interactables outside this range use interpolation to save processing power
- The transition between states is handled automatically by
InteractablePredictionSwitchingSystem
public float InteractionPredictionRange { get; set; } = 10f;
Implementation Example
Here's how to set up a basic interactor for a player:
var entity = GetEntity(TransformUsageFlags.Dynamic);
AddComponent(entity, new PlayerInteractorSpawner
{
PrefabEntity = GetEntity(interactorPrefab, TransformUsageFlags.Dynamic),
PositionOffset = new float3(0, 0, 0.5f)
});
Troubleshooting
Common issues and solutions:
- Interactions not responding:
- Verify interactor spawner is properly configured
- Check that interactables are properly configured
- Ensure prediction range is appropriate
- Performance issues:
- Adjust prediction range to balance responsiveness and performance
- Monitor number of predicted interactables
- Consider reducing interaction distance for better performance