Discussed in the question here:
I noticed the comment that you may want to use two buttons. I have repeatadly tried this but it's giving back unexpected behaviour ( Yes I checked the coding round and about 2592 times now) .
Here's the sample C# VS2010 .NET 2.0 (Yes I know [Flags] enumeration is better but it,s the .NET 2.0 I'm afraid which doesn't support that , my code has to be iPrimitive) :
The sample is for when a user HOLDS DOWN a key , it checks by comparing the states this frame and last frame.
if (!RightKeyPressed_Old)
{
if (RightKeyPressed_New)
{
//Input states character wants to Run right
isRunning = true;
}
if (!RightKeyPressed_New)
{
//Input states character wants to stop Running right
isRunning = false;
}
}
//With nesting: If Left Key is being held down! If it's NOT being held Down!
if (!LeftKeyPressed_Old)
{
if (LeftKeyPressed_New)
{
//Input states character wants to Run
isRunning = true;
}
if (!LeftKeyPressed_New)
{
//Input states character wants to stop Running left
isRunning = false;
}
}
The code simply then checks in another method if the state is true to play the respective running animation. All I have to do then is change the character orintation in the 2D environment.
My Debug log clearly states the key was pressed and released however it is ONLY responding to ONE of the two. If I put one code block on top has no effect. The inputmanagersettings are not bugged and do match. The same is happening for left/right shift and left/right ctrl ( I tested it )
I'm not so worried anymore it's my code therefor, and more worried it's a way Unity3D thinks I should do inputhandling in a certain way with the manager which I'm just not getting.
Not confused yet? Well here's a quick summary:
I press left or right , and the response is NOT the same!! It's the same problem with left shift and left ctrl and the code seems to be working according to my debugging.
Please help me find a solution. I am willing to just toss it overboard and work with the negative values of a pressed buttong, but I don't want to be using GetAxis or GetAxisRaw.
Thanks in advance!
EDIT 11/2/2010:
I think I just found something. I may have just ended up performing a bypass giving me incorrect results. (Meaning: I changed the input manager values of the virtual buttons into something silly like T and Y buttons) and guess what. No response. So i may simply have assigned the wrong variable. Checking for that now. Will post results if so.
EDIT 11/2/2010: 2 minutes later
Nope, it just seems to accept left and right as input even though there is no button defined for it. When I tell it to use t and y on the object aswell as the input manager. The peculiar thing is. I really think I'm running out of options.
EDIT 11/2/2010: 10 minutes later
Think the code really was the same but that was actually the problem, I wanted to get a dumb version to run first and didn't care about left or right, but left was changing right. The problem moves if I invert the code block above and down, which I said it didn't , but I probably just forgot to save!
//Pressed this frame?
if (Input.GetKeyDown(LeftKey))
{
Debug.Log("LeftKey Was Pressed This Frame");
LeftKeyPressed_Old = true;
LeftKeyPressed_New = true;
}
if (Input.GetKeyUp(LeftKey))
{
Debug.Log("LeftKey Was Released This Frame");
LeftKeyPressed_New = false;
}
//Pressed this frame?
if (Input.GetKeyDown(RightKey))
{
Debug.Log("RightKey Was Pressed This Frame");
RightKeyPressed_Old = true;
RightKeyPressed_New = true;
}
if (Input.GetKeyUp(RightKey))
{
Debug.Log("RightKey Was Released This Frame");
RightKeyPressed_New = false;
}
FINAL EDIT: 11/3/2010
Changing the logic to this fixed it:
if (HasAbilityToRun)
{
if (!LeftKeyPressed_Old || !RightKeyPressed_Old)
{
//if a run left key was hit
if (LeftKeyPressed_New)
{
//Input states character wants to Run
isRunning = true;
//CHANGE CHARACTER ORIENTATION TO LEFT
//Run();
}
//if a run right key was hit
if (RightKeyPressed_New)
{
//Input states character wants to Run right
isRunning = true;
//CHANGE CHARACTER ORIENTATION TO RIGHT
//Run();
}
if (!LeftKeyPressed_New && !RightKeyPressed_New)
{
//Input states character wants to stop Running right
isRunning = false;
//STOP RUNNING
}
}
}