I design a character can shoot in four direction: up down left right. By using four button: W S A D. The issue is that , when i push those buttons together the character can shoot in direction at same. But what i want is when i push one button the character only can shoot in that direction .I don`t want my character can shot in different directions at same time. How can i fix this.Please assist .
This is my code:
One solution could be to use else if (instead of 4 distinct if statements). The user must release the current button to be able to shoot into another direction.
e.g.
if(Input.GetButtonDown("FireRight"))
{
Rigidbody2D bulletInstance = Instantiate(bullet, transform.position, Quaternion.Euler(new Vector3(0,0,0))) as Rigidbody2D;
bulletInstance.velocity = new Vector2(speed, 0);
}
else if(Input.GetButtonDown("FireLeft"))
{
Rigidbody2D bulletInstance = Instantiate(bullet, transform.position, Quaternion.Euler(new Vector3(0,0,180))) as Rigidbody2D;
bulletInstance.velocity = new Vector2(-speed, 0);
}
else if(Input.GetButtonDown("FireUp"))
{
Rigidbody2D bulletInstance = Instantiate(bullet, transform.position, Quaternion.Euler(new Vector3(0,0,90))) as Rigidbody2D;
bulletInstance.velocity = new Vector2(0, speed);
}
else if(Input.GetButtonDown("FireDown"))
{
Rigidbody2D bulletInstance = Instantiate(bullet, transform.position, Quaternion.Euler(new Vector3(0,0,270))) as Rigidbody2D;
bulletInstance.velocity = new Vector2(0, -speed);
}
Thanks for ur adivce. I tried ur code but it still can shoot different directions at same time.But ur idea is helpful. So i can make my button like this: disablle other three buttons when i push one.and active those button after i released. But i don`t know the codes which can disable and reactive buttons. If u know , could u teach me that. XD