- Home /
button sequence command, how to??
Hello there, I just wanna know if is there an easy way to use a key sequence on unity. Ex
left + left = run
left + left + down + up + fire = fatality1
things like that.
thanks in advance!
Answer by rudolfwm · Feb 19, 2011 at 02:31 PM
Use the following class:
public class KeySequence { public delegate void HandleCallback(KeySequence pSequence); List<string> mKeyList = new List<string>(); int mProgressIndex = 0 ; HandleCallback mHCB = null;
public KeySequence(HandleCallback pCallbackFunction)
{
mHCB = pCallbackFunction;
}
public void AddKey2Sequence(string pKeyStr)
{
mKeyList.Add(pKeyStr);
}
public void Update()
{
if(mKeyList.Count==0)
{
return ;
}
if(Input.GetKey(mKeyList[mProgressIndex]))
{
mProgressIndex++;
if(mProgressIndex>=mKeyList.Count)
{
if(mHCB!=null)
{
mProgressIndex=0;
mHCB(this);
}
}
}
else
{
mProgressIndex=0;
}
}
}
Now you can make a keysequence like this:
KeySequence lTestSequence = new KeySequence(new KeySequence.HandleCallback(TestSequencePressed));
lTestSequence.AddKey2Sequence("left");
lTestSequence.AddKey2Sequence("right");
lTestSequence.AddKey2Sequence("left");
Then you put lTestSequence.Update() in your update routine.
Now when the sequence is correctly entere the function TestSequencePressed will be called with the sequence object that fired. You will have to declare this function somewhere of course :)
Sorry, i am very noob in program$$anonymous$$g, i just know javascript.
Answer by NewfieJoe · Feb 19, 2011 at 02:27 PM
You could use a string as an input buffer with each input adding a command to the end and removing one from the beginning then you only have to do checks based on the last command in the sequence. So if it's checking "fire" it can parse the string at the beginning of the "fire" function and see if the previous inputs were "left-left-down-up-fire", if so branch off to the alternate function for the fatality.
Answer by masfelix · Jun 15, 2011 at 05:16 PM
I put de code:
KeySequence lTestSequence = new KeySequence(new KeySequence.HandleCallback(TestSequencePressed)); lTestSequence.AddKey2Sequence("left"); lTestSequence.AddKey2Sequence("right"); lTestSequence.AddKey2Sequence("left");
in FixedUpdate, but i receive a erro in delegate.
A create a function named TestSequencePressed,I put KeySequence lTestSequence = new KeySequence(new KeySequence.HandleCallback(TestSequencePressed)); lTestSequence.AddKey2Sequence("left"); lTestSequence.AddKey2Sequence("right"); lTestSequence.AddKey2Sequence("left");
In Function FixedUpdate(), but she has a error in delegate, Why?
Answer by $$anonymous$$ · May 01, 2012 at 04:57 PM
Here : unitynoobs you can download a small project with this answer!
Your answer
Follow this Question
Related Questions
multiple inputs 2 Answers
Follow An Object Until Key Press 2 Answers
How do I make a UI button to click a key? 1 Answer
How to get the key from the button name 1 Answer
How to time when a button is pressed 1 Answer