I want to make a dash system like in kirby and smash bros (i’m making a fighter game combining both). I’m having difficulties going from a walking animation, to a running animation when a right or left key is tapped twice. Here is the code I have so far:
var keyCounter : int = 0;
var keyUp : boolean = false;
var running : boolean = false;
var walking : boolean = false;
function Update ()
{
var aniPlay = GetComponent("aniSprite");
keyCounter = 0;
if (Input.GetKey("right") && !running ) //walk right
{
print ("keyCounter = " + keyCounter);
aniPlay.aniSprite (24,26,0,1.75,12,15);
keyCounter = 1;
walking = true;
}
if (Input.GetKeyUp ("right") && keyCounter == 1)
{
if ( Time.time < 0.5 ) //tapped within half a second
{
print ("keyCounter = " + keyCounter);
keyCounter = 2;
walking = false;
}
else //cancel walk/run
{
print ("keyCounter = " + keyCounter);
walking = false;
keyCounter = 0;
}
}
if (Input.GetKeyDown("right") && keyCounter == 2)
{
if (Input.GetKey ("right") && !walking)
{
running = true;
aniPlay.aniSprite (24,26,1,3.25,7,15);
print ("i am running " + keyCounter);
}
if (Input.GetKeyUp ("right") && keyCounter == 2) // run right
{
print ("keyCounter = " + keyCounter);
running = false;
keyCounter = 0;
}
}
}
I’ve looked at other examples of a double tap but it still hasn’t helped me.
Any suggestions or tips on how to fix this?
// double tap variable
var time1: float;
var time2: float;
var isTap : boolean = false;
function Update ()
{
if (Input.GetMouseButton(0)) // when mouse is clicked
{
if (isTap == true)
{
time1 = Time.time;
isTap = false;
if (time1 - time2 < 0.2f) // interval between two clicked
{
// double tap occur here
}
}
}
else // first of all, enter here because the mouse is not clicked
{
if (isTap == false)
{
time2 = Time.time;
isTap = true;
}
}
}
You can use this class with any key and deltatime.
You can increase perfomance using Time.timeSinceLevelLoad instead Time.deltaTime, but you will have float accuracy problem after 115 days of playing)
private DoubleClicker tabDoubbleCatch = new DoubleClicker(KeyCode.Tab);
private void Update()
{
if (tabDoubbleCatch.DoubleClickCheak())
{
//some code
}
}
public class DoubleClicker
{
/// <summary>
/// Construcor with keycode and deltaTime set
/// </summary>
public DoubleClicker(KeyCode key, float deltaTime)
{
//set key
this._key = key;
//set deltaTime
this._deltaTime = deltaTime;
}
/// <summary>
/// Construcor with defult deltatime
/// </summary>
public DoubleClicker(KeyCode key)
{
//set key
this._key = key;
}
private KeyCode _key;
private float _deltaTime = defultDeltaTime;
//defult deltaTime
public const float defultDeltaTime = 0.3f;
/// <summary>
/// Current key property
/// </summary>
public KeyCode key
{
get { return _key; }
}
/// <summary>
/// Current deltaTime property
/// </summary>
public float deltaTime
{
get { return _deltaTime; }
}
//time pass
private float timePass = 0;
/// <summary>
/// Cheak for double press
/// </summary>
public bool DoubleClickCheak()
{
if (timePass > 0) { timePass -= Time.deltaTime; }
if (Input.GetKeyDown(_key))
{
if (timePass > 0) { timePass = 0; return true; }
timePass = _deltaTime;
}
return false;
}
}
I don’t like how this code seems too cluttered to me, but for people who need it, this lets you check if a button was pressed once, twice or if it is being held and when it’s released after holding the button.
int ButtonCount = 0;
float t;
float holdThres = .8f; // threshold for checking if button is being held
float doubleTrhes .5f; //threshold for checking if button is being pressed twice
void Update () {
string button = "Fire 1";
if (Input.GetButtonDown (button)) {
if (ButtonCount == 0) {
t = Time.time;
}
ButtonCount += 1;
}
if ((Time.time - t) > holdThres && Input.GetButton (button) && ButtonCount == 1) {
Debug.Log ("Is currently holding [" + button + "]");
} else if ((Time.time - t) > holdThres && Input.GetButtonUp (button) && ButtonCount == 1) {
Debug.Log ("Stopped holding [" + button + "]");
ButtonCount = 0;
} else if ((Time.time - t) < doubleTrhes && ButtonCount == 2) {
Debug.Log ("Double tapped [" + button + "]");
ButtonCount = 0;
} else if ((Time.time - t) > doubleTrhes && ButtonCount == 1 && !Input.GetButton (button)) {
Debug.Log ("Tapped [" + button + "] once");
ButtonCount = 0;
}
}