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;
}
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.
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.
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.
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.
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.