I’m trying to make a smash bros clone game and have been stuck on this particular problem for weeks now. So you know how in Smash Bros each direction arrow can be combined with a special button to produce different attacks. The question is what would be the best way to read that kind of button input in Unity? Thanks in advance for any help
do you want to do full combos like “up, down, left, lightattack” or just check on a light attack what direction your joystick is pointing?
here is an rough implementation:
save your direction in a vector2. this has x and y. for example if y>0 it is up, if its <0 it is down. when your button is pressed you check your vector. alternatively you can just use Input.GetAxis(“Horizontal”); for reading in which direction the player inputs are. and update that in this coroutine.
public class inputstuff : MonoBehaviour {
void Start() {
StartCoroutine (CheckInputs()); //check what other input is followed
}
Vector2 yourinput;
public bool sattackNORMAL,sattackRIGHT,sattackLEFT,sattackUP,sattackDOWN;
IEnumerator CheckInputs()
{ while(true){
yourinput.x = Input.GetAxis("Horizontal");
yourinput.y = Input.GetAxis("Vertical");
if ((Mathf.Abs(yourinput.magnitude) < 0.002f)) sattackNORMAL=true; //no direction=normalattack;
else{
if (yourinput.x>= 0.01f) sattackRIGHT = true; //right attack
else if(yourinput.x<= -0.01f) sattackLEFT=true; //left attack
else if (yourinput.y>= 0.01f) sattackUP = true; //up attack
else sattackDOWN=true; //down attack
}
yield return new WaitForSeconds(.1f);
}
}
}
I would highly recommend you look into state machines (there are plenty of tutorials for Unity state machines). All of your button input combinations become states.
To represent a “Fully Charged Down Smash”, for example:
- Your Player might start in an Idle (or Walk or Run) state.
- These states listen for a “down” input and transition to a Crouch (or CrouchSlide) state
- The Crouch state listens for a press-and-hold on the attack button, moving to DownSmashCharge
- When either a charge timer is expired, or the button is released, transition to DownSmash.
- Transition back to Idle
This could either be coupled with, or made independent of, the Unity Animator state machine. I’d recommend doing it separately.
Thank you all for responding, really appreciated all your helps. So the game I’m working on is a platform fighting game with similar controls to Smash Bros. What I’m trying to do with my character is like this:
When a attack button is pressed (check in update()) → check what arrow key is pressed right after that → register appropriate bools. Here’s a snippet of my input handler script according to @mafima :
public class InputHandler : MonoBehaviour {
Vector2 inputVector; //this is the input vector
void Update()
{
horizontal = Input.GetAxis("Horizontal"); //read horizontal and vertical axis in update
vertical = Input.GetAxis ("Vertical");
if (Input.GetButtonDown("Fire2")) //if the first attack button is pressed -> go to check if any arrow key is followed
{
StartCoroutine (CheckInput);
}
UpdateAllBools (); //pass all the bools to another script that does other work
ResetAllBools (); //set all bools to false at the end of each frame
}
IEnumerator CheckInput() //This is the same as your suggestion script
{
inputVector.x = Input.GetAxis ("Horizontal" + playerInput);
inputVector.y = Input.GetAxis ("Vertical" + playerInput);
if (Mathf.Abs (inputVector.magnitude) <= 0.02f)
{
attack2 = true;
Debug.Log ("reg attack");
}
else
{
if (Mathf.Abs (inputVector.x) >= 0.2f)
{
sattack2 = true;
Debug.Log ("side attack");
}
else
{
if (inputVector.y >= 0.2f)
{
sattack3 = true;
Debug.Log ("up attack");
}
else
{
if (inputVector.y <= -0.2f)
{
sattack4 = true;
Debug.Log ("down attack");
}
}
}
}
yield return new WaitForSeconds (0.2f);
}
}
still doesn’t work. I apologize for keep asking on this but could anyone please help me with a script that work like I mentioned? My project has been on paused for 2 weeks now because of this