- Home /
Problem with inputs in command system
Hi all,
I have an annoying problem that has to do with Inputs. So I am making a simple Shooter game of my own, and the moving system is implemented via a command pattern. My keyboard inputs handler does seem to working well... Let me explain: the ship can move left, right, up and down, and it can fire. When the ship is not firing, I can move in all directions allright, diagonals and all. The problem arises when I am firing and try move in the top left diagonal... The ship either goes left or up depending on the key pressed first, but not diagonal. Worse, the key pressed is not even detected, so it is not a problem of generating the command. I have tried to work around the problem but I was somehow unable to solve it. The code of my Command handler function is bellow:
public override List<Command> HandleInput()
{
List<Command> res = new List<Command>(4);
//1- Retrieve the movement information
//tried with direct access via keycodes
bool up = Input.GetKey(KeyCode.UpArrow);
bool down = Input.GetKey(KeyCode.DownArrow);
bool right = Input.GetKey(KeyCode.RightArrow);
bool left = Input.GetKey(KeyCode.LeftArrow);
/*float vert = Input.GetAxis ("Vertical");
float hor = Input.GetAxis("Horizontal");
Debug.Log (vert + "\t" + hor);
m_movement.x = hor;
m_movement.y = vert;*/
//Tried with using personalised commands
/*bool up = Input.GetButton("Up");
bool down = Input.GetButton("Down");
bool right = Input.GetButton("Right");
bool left = Input.GetButton("Left");*/
Debug.Log(up+"\t"+down+"\t"+left+"\t"+right);
if (up && m_object.rigidbody2D.position.y < y_min)
{
m_movement.y = 1f; //m_movement is a command object
}
else if(down && m_object.rigidbody2D.position.y > y_max)
{
m_movement.y = -1f;
}
else
{
m_movement.y = 0f;
}
if (right && m_object.rigidbody2D.position.x < x_max)
{
m_movement.x = 1f;
}
else if(left && m_object.rigidbody2D.position.x > x_min)
{
m_movement.x = -1f;
}
else
{
m_movement.x = 0f;
}
if(!(m_movement == m_old_movement))
{
// Create the command movement
m_moveCommand.Velocity = m_movement;
res.Add(m_moveCommand);
m_old_movement = m_movement;
}
//2- Check if the firing button has gone down
if(Input.GetButtonDown("Fire"))
{
res.Add(m_startFiringCommand);
}
//3- check if the fire button has gone up
if(Input.GetButtonUp("Fire"))
{
res.Add(m_stopFiringCommand);
}
//4- Check if the pause button is hit
if(Input.GetButtonDown("Pause"))
{
res.Add(m_pauseCommand);
}
//5- return the command list
return res;
}
Does anyone has any idea? Please don't hesitate to ask me for more information or for clarification. If the code is not enough, I'll send you a link to the work in progress version of the game...
EDIT UP :-)
Your answer