Search Unity

Input Not Getting Detected

Discussion in 'Scripting' started by FGPArthurVII, Jul 15, 2020.

  1. FGPArthurVII

    FGPArthurVII

    Joined:
    Jan 5, 2015
    Posts:
    106
    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.

    Code (CSharp):
    1. using Unity.Entities;
    2. using Unity.Mathematics;
    3. using UnityEngine;
    4.  
    5. [GenerateAuthoringComponent]
    6. public struct MoveData : IComponentData
    7. {
    8.     public float3 direction;
    9.     public float speed;
    10.     public float2 mouseSensitivety;
    11. }
    Code (CSharp):
    1. using UnityEngine;
    2. using Unity.Entities;
    3.  
    4. [GenerateAuthoringComponent]
    5. public struct InputData : IComponentData
    6. {
    7.     public KeyCode upKey;
    8.     public KeyCode downKey;
    9.     public KeyCode leftKey;
    10.     public KeyCode rightKey;
    11. }
    Code (CSharp):
    1. using Unity.Entities;
    2. using System;
    3. using UnityEngine;
    4.  
    5. public class PlayerInputSystem : SystemBase
    6. {
    7.    protected override void OnUpdate()
    8.    {
    9.  
    10.        if(Input.GetKey(KeyCode.A))
    11.        {
    12.            Debug.Log("Uhhh...Hello?");
    13.        }
    14.  
    15.        Entities
    16.             .ForEach((ref MoveData moveData, in InputData inputData) =>
    17.             {
    18.                 bool isUpKeyPressed = Input.GetKey(inputData.upKey);
    19.                 bool isDownKeyPressed = Input.GetKey(inputData.downKey);
    20.                 bool isLeftKeyPressed = Input.GetKey(inputData.leftKey);
    21.                 bool isRightKeyPressed = Input.GetKey(inputData.rightKey);
    22.  
    23.                 moveData.direction.z = Convert.ToInt32(isUpKeyPressed);
    24.                 moveData.direction.z -= Convert.ToInt32(isDownKeyPressed);
    25.                 moveData.direction.x -= Convert.ToInt32(isLeftKeyPressed);
    26.                 moveData.direction.x = Convert.ToInt32(isRightKeyPressed);
    27.  
    28.                 if(isLeftKeyPressed)
    29.                 {
    30.                     Debug.Log("Stop Pressing Me!");
    31.                 }
    32.  
    33.             })
    34.             .Run();
    35.    }
    36. }
    37.  
    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
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,752
    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!!!
     
  3. FGPArthurVII

    FGPArthurVII

    Joined:
    Jan 5, 2015
    Posts:
    106
    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.
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,752
    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.
     
  5. FGPArthurVII

    FGPArthurVII

    Joined:
    Jan 5, 2015
    Posts:
    106
    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.
     
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,752
    I dunno man, that seems convoluted to me. Why can't you just conditionally assign it?
     
    bobisgod234 likes this.
  7. bobisgod234

    bobisgod234

    Joined:
    Nov 15, 2016
    Posts:
    1,042
    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

    Code (CSharp):
    1.                 moveData.direction.z += Convert.ToInt32(isUpKeyPressed);
    2.                 moveData.direction.z -= Convert.ToInt32(isDownKeyPressed);
    3.                 moveData.direction.x -= Convert.ToInt32(isLeftKeyPressed);
    4.                 moveData.direction.x += Convert.ToInt32(isRightKeyPressed);
     
    FGPArthurVII likes this.
  8. FGPArthurVII

    FGPArthurVII

    Joined:
    Jan 5, 2015
    Posts:
    106

    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!
     
  9. bobisgod234

    bobisgod234

    Joined:
    Nov 15, 2016
    Posts:
    1,042
    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.
     
    Kurt-Dekker likes this.
  10. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,752
    Code (csharp):
    1. x = 0;
    2. y = 0;
    3.  
    4. if (Input.GetKey(inputData.upKey)) y = 1;
    5. if (Input.GetKey(inputData.downKey)) y = -1;
    6. // etc.
    Don't buy yourself needless complexity. Indeed as The Godly Bob said above, KISS.
     
    Last edited: Jul 15, 2020
    bobisgod234 likes this.