Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question Why Input.GetKeyDown not working as expected with (IInputComponentData)?

Discussion in 'NetCode for ECS' started by OrientedPain, Aug 12, 2023.

  1. OrientedPain

    OrientedPain

    Joined:
    Mar 26, 2018
    Posts:
    41
    Hello everyone,

    I made a GatherAutoCommandsSystem (Client Side) that gathers all player inputs and stores them in IInputComponentData, the movement works fine when using Input.GetKey, but when using Input.GetKeyDown to fire a bullet when player press space bar (only one bullet per press) the command did not send to ProcessAutoCommand (Server side) until I press more than one time.

    Code (CSharp):
    1.  public void OnUpdate(ref SystemState state)
    2. {
    3.      bool left = UnityEngine.Input.GetKey("left") || UnityEngine.Input.GetKey("a");
    4.      bool right = UnityEngine.Input.GetKey("right") || UnityEngine.Input.GetKey("d");
    5.      bool down = UnityEngine.Input.GetKey("down") || UnityEngine.Input.GetKey("s");
    6.      bool up = UnityEngine.Input.GetKey("up") || UnityEngine.Input.GetKey("w");
    7.      bool attack = UnityEngine.Input.GetKeyDown("Space");
    I have two questions:

    1- How to make the server receive a single press from clients to server (without using PredictedSimulationSystemGroup)? I have the same issue with ICommandData.

    2- Should I use PredictedSimulationSystemGroup with Damage Logic (I separated damage logic from client side)? PredictedSimulationSystemGroup will force me to make damage logic exist in both client and server.

    Thanks in advance.
     
    Last edited: Aug 12, 2023
  2. OrientedPain

    OrientedPain

    Joined:
    Mar 26, 2018
    Posts:
    41
    I'm using InputEvent to be able to use triggers and events.

    Code (CSharp):
    1. inputData.ValueRW = default;
    2.  
    3. if (left)
    4.     inputData.ValueRW.Horizontal -= 1;
    5.  
    6. if (right)
    7.     inputData.ValueRW.Horizontal += 1;
    8.  
    9. if (down)
    10.     inputData.ValueRW.Vertical -= 1;
    11.  
    12. if (up)
    13.     inputData.ValueRW.Vertical += 1;
    14.  
    15. if (attack)
    16.     inputData.ValueRW.SkillAttack.Set();
     
    Last edited: Aug 12, 2023
    NikiWalker likes this.
  3. unity_VigorBioFunStudio

    unity_VigorBioFunStudio

    Joined:
    May 10, 2019
    Posts:
    10

    +1 Same Issue Here.

    I separated some logic from client and trying to send input event from client (IInputComponentData) to server (WorldSystemFilterFlags.ServerSimulation), but event does not send from first press.
     
  4. NikiWalker

    NikiWalker

    Unity Technologies

    Joined:
    May 18, 2021
    Posts:
    241
    InputEvent (used on an IInputComponentData component struct) was built for this case.

    As OrientedPain says, use the .Set method. Calling this will increment a counter, allowing the server to distinguish between each press by comparing the values.
     
  5. optimise

    optimise

    Joined:
    Jan 22, 2014
    Posts:
    2,046
    Hi. Does InputEvent also use it to solve the problem that sometimes server failed to detect input like button press? If yes can official support setup custom input data into InputEvent like uint? Is that still not enough to make it always able to detect button input with just IInputComponentData?
     
    Last edited: Aug 28, 2023
  6. optimise

    optimise

    Joined:
    Jan 22, 2014
    Posts:
    2,046
    @CMarastoni I would like to know that why even I already use IInputComponentData, sometimes it still failed to detect input from IInputComponentData and just miss the input?