Hello everyone,
It’s probably due to a misconfiguration on my part, but I’m going crazy trying to solve a problem I can’t figure out. I’m developing a multiplayer point-and-click game. I’ve set up my player entity with the ghost component.
I’m using the new Input System. On the Client side, I have a
SystemBase
that captures mouse clicks.
private void ClickedMouse(InputAction.CallbackContext context)
{
float2 mousePosition2D = Mouse.current.position.ReadValue();
Logger.WriteLog($"Clicked Mouse at position: {mousePosition2D}");
var PhysicsWorld = SystemAPI.GetSingleton<PhysicsWorldSingleton>().PhysicsWorld;
var collisionWorld = SystemAPI.GetSingleton<PhysicsWorldSingleton>().CollisionWorld;
var cameraEntity = SystemAPI.GetSingletonEntity<MainCameraInGameTag>();
var mainCamera = EntityManager.GetComponentData<MainCameraInGame>(cameraEntity).value;
float3 mousePosition = new float3(mousePosition2D.x, mousePosition2D.y, 100f);
var worldPosition = mainCamera.ScreenToWorldPoint(mousePosition);
if (_inputActions.Game.LeftClick.WasPressedThisFrame())
inputEvent.Set(); // Imposta il flag IsSet a true
var PlayerEntity = SystemAPI.GetSingletonEntity<PlayerInGameComponent>();
EntityManager.SetComponentData(PlayerEntity, new PlayerInputComponent
{
StartRayClick = mainCamera.transform.position,
EndRayClick = worldPosition,
ActionClicked = inputEvent
});
The IInputComponentData component correctly replicates the values I pass to it, and the action setup is also correct.
On the server side, I receive the component from an ISystem:
[UpdateInGroup(typeof(PredictedSimulationSystemGroup))]
[WorldSystemFilter(WorldSystemFilterFlags.ServerSimulation)]
[BurstCompile]
public partial struct ProcessInputSystem : ISystem
{
[BurstCompile]
public void OnCreate(ref SystemState state)
{
state.RequireForUpdate<NetworkTime>();
state.RequireForUpdate<PlayerInputComponent>();
}
public void OnUpdate(ref SystemState state)
{
var networkTime = SystemAPI.GetSingleton<NetworkTime>();
if (!networkTime.IsFirstTimeFullyPredictingTick)
return;
// Log dettagliato per monitorare il ciclo di previsione
foreach (var (inputComponent, player, entity) in SystemAPI.Query<RefRW<PlayerInputComponent>, RefRO<PlayerInGameComponent>>().WithEntityAccess().WithAll<Simulate>())
{
Debug.Log($"NetworkTime {entity}");
bool isEnabled = inputComponent.ValueRO.ActionClicked.IsSet;
//Debug.Log($"ProcessInputSystem Player: {player.ValueRO.PlayerID} :: IsSet {isEnabled} :: {state.World}");
// Process the input here
if (isEnabled)
{
Debug.Log($"NetworkTime Status: ServerTick={networkTime.ServerTick} frame {UnityEngine.Time.frameCount} IsFirstTimeFullyPredictingTick {networkTime.IsFirstTimeFullyPredictingTick}");
// Esegui l'azione desiderata quando l'input è attivo
//Debug.Log($"ProcessInputSystem Player " +
// $"{player.ValueRO.PlayerID} clicked {networkTime.IsFirstTimeFullyPredictingTick}");
}
}
}
The input component is defined as follows:
using Unity.Mathematics;
using Unity.NetCode;
[GhostComponent]
public struct PlayerInputComponent : IInputComponentData
{
[GhostField(Quantization = 0)] public float3 StartRayClick;
[GhostField(Quantization = 0)] public float3 EndRayClick;
[GhostField(Quantization = 0)] public InputEvent ActionClicked;
}
The problem is that when I click the mouse button, on the server the IsFirstTimeFullyPredictingTick
flag is always set to true
, causing the function to be called 24 times.
I can’t figure out what I’m doing wrong or how to move forward. Has anyone experienced the same issue or knows how I can fix it?