Input Not Getting Detected

I am making a movement system but for some weird reason, the LeftKey and only the LeftKey is not working, It does not return any errors It simply does nothing.

using Unity.Entities;
using Unity.Mathematics;
using UnityEngine;

[GenerateAuthoringComponent]
public struct MoveData : IComponentData
{
    public float3 direction;
    public float speed;
    public float2 mouseSensitivety;
}
using UnityEngine;
using Unity.Entities;

[GenerateAuthoringComponent]
public struct InputData : IComponentData
{
    public KeyCode upKey;
    public KeyCode downKey;
    public KeyCode leftKey;
    public KeyCode rightKey;
}
using Unity.Entities;
using System;
using UnityEngine;

public class PlayerInputSystem : SystemBase
{
   protected override void OnUpdate()
   {

       if(Input.GetKey(KeyCode.A))
       {
           Debug.Log("Uhhh...Hello?");
       }

       Entities
            .ForEach((ref MoveData moveData, in InputData inputData) =>
            {
                bool isUpKeyPressed = Input.GetKey(inputData.upKey);
                bool isDownKeyPressed = Input.GetKey(inputData.downKey);
                bool isLeftKeyPressed = Input.GetKey(inputData.leftKey);
                bool isRightKeyPressed = Input.GetKey(inputData.rightKey);

                moveData.direction.z = Convert.ToInt32(isUpKeyPressed);
                moveData.direction.z -= Convert.ToInt32(isDownKeyPressed);
                moveData.direction.x -= Convert.ToInt32(isLeftKeyPressed);
                moveData.direction.x = Convert.ToInt32(isRightKeyPressed);

                if(isLeftKeyPressed)
                {
                    Debug.Log("Stop Pressing Me!");
                }

            })
            .Run();
   }
}

So, the if Debug Log inside the ForEach loop doesn’'t print out anything at all. Meanwhile the Debug Log for the same LeftKey outside the ForEach does print out. (I’ve also tried to use the same command as outside inside the foreach (GetKey(KeyCode.A)) but too did not work) The bizarre thing is every other key works perfectly.

I’ve even ried to change the assigned key, changing the order of the Input lines in the code but anyway the code still fails to detect the LeftKey pressing and It alone.

Thanks;
Arthur

Why not take it a few steps further and print the value of inputData.leftKey right there inside your inner loop?

DEBUG LOG ALL THE THINGS!!!111!!!

The Entity on the Entity Debugger shows me a Authoring Component of the MoveData class . The direction float3 changes the value of X or Z to 1 or -1 when It is pressed. the X value should turn 1 for right key (which It indeed does) but when It comes to the Left Key It should turn -1, but instead It just keeps 0 be It pressed or unpressed.

oh line 24 and line 25… you’re saying -= when you mean = -

I presume you intend to negate the return from convert and assign it.

The Idea is to subtract one -= will make the should go -1 so It moves negative velocity (So left/right), not only that, It should do as If I pressed both LeftKey and RightKey at the same time It will calculate 0 (Standard Value) + 1 - 1 = 0 and does not go anywhere.

So, -= is correct, In fact It works as Intended for line 24, doing this very process perfectly for Ahead/Backwards.

I’ve tried to change them to = - as you adviced too, but upon doing that both of them stopped working.

I dunno man, that seems convoluted to me. Why can’t you just conditionally assign it?

1 Like

It works for y because you are doing the subtraction after the assignment. For x, you are doing the subtraction before the assignment. Since you are always assigning to x on line 26, line 25 has no effect on the final value of x.

Try changing the assignments to be +=, like so

                moveData.direction.z += Convert.ToInt32(isUpKeyPressed);
                moveData.direction.z -= Convert.ToInt32(isDownKeyPressed);
                moveData.direction.x -= Convert.ToInt32(isLeftKeyPressed);
                moveData.direction.x += Convert.ToInt32(isRightKeyPressed);
1 Like

It did not work by changing the = to +=, as the object started moving forever instead of on pressing, however, looking at the before and after thing you’ve mentioned, I’ve made de LeftKey Convert Ocurr after the RightKey Convert as It is for Up and Down as you mentioned, now everything is working perfectly. Thank you!

I would suggest considering conditionally assigning the values, as Kurt-Dekker suggested. No need to use Convert.ToInt32 and make your code harder to understand. KISS, and all that.

1 Like
x = 0;
y = 0;

if (Input.GetKey(inputData.upKey)) y = 1;
if (Input.GetKey(inputData.downKey)) y = -1;
// etc.

Don’t buy yourself needless complexity. Indeed as The Godly Bob said above, KISS.

1 Like