- Home /
How do I make a double-tap system for dashing
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?
Answer by Montraydavis · Oct 31, 2012 at 04:34 AM
var ButtonCooler : float = 0.5 ; // Half a second before reset
var ButtonCount : int = 0;
function Update ( )
{
if ( Input.anyKeyDown ( ) ){
if ( ButtonCooler > 0 && ButtonCount == 1/*Number of Taps you want Minus One*/){
//Has double tapped
}else{
ButtonCooler = 0.5 ;
ButtonCount += 1 ;
}
}
if ( ButtonCooler > 0 )
{
ButtonCooler -= 1 * Time.deltaTime ;
}else{
ButtonCount = 0 ;
}
}
That detects if you double tap any two keys within 0.5 seconds. Change it to fit your needs.
If this has answered your issue, please hit the check mark to represent as answered. Thanks for supporting Unity3D ! :D
@ $$anonymous$$ontraydavis Thank you so much! I'll give you credit for this code in my game :)
Thank you. And you are very welcome. If you ever need help, feel free to ask at ANYTI$$anonymous$$$$anonymous$$ Let me know the progress on your game. I want to see how it looks once you get to a beta point, if you don't $$anonymous$$d. Best of luck to you, and thanks for using Unity3D.!
--PS: I didn't mention in the post above, but, you can change the number of taps you want at ( == 1 ) . It will always be one less than what you want. I just updated the code .
Dude, I hope you don't $$anonymous$$d, but I was having a similar problem creating something similar. I am working in a beat'em up kind of game and I needed to create something for combo chains. Your code gave me the ground I needed to work with, although I work in C#, so I did some changes to it and made into a triple click thing where each time you click something different is going to happen. I will post it, since I could not have it done it without yours and in case someone else needs help with something similar. Without further ado here we go.
//Originally written by $$anonymous$$ontraydavis //Code taken from the answers forums in the Unity website //Originally it was written in javaScript, but I changed into C# //It is also worth nothing that it was originally meant to detect double click actions //Therefore I changed it to be used to create a combo chain kind of thing //It will detect triple clicks //In other words, each time you click a different action will be performed
private int clickCount = 0;
private int mouseTimer = 0;
void attackTest()
{
if(Input.GetButtonDown("Fire1"))
{
if(clickCount == 0)//Number of tabs you want $$anonymous$$us one
{
anim.SetTrigger("Attack");
}
if(mouseTimer > 0 && clickCount == 1)//Number of tabs you want $$anonymous$$us one
{
anim.SetTrigger("Attack01");
}
if (mouseTimer > 0 && clickCount == 2)//Number of tabs you want $$anonymous$$us one
{
anim.SetTrigger("Attack02");
}
else
{
mouseTimer = 0.5f;
clickCount += 1;
}
}
if(mouseTimer > 0)
{
mouseTimer -= 1 * Time.deltaTime;
}
else
{
clickCount = 0;
}
}
Answer by syamilsynz · Feb 22, 2014 at 09:14 PM
// 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;
}
}
}
Answer by ajnaduvil · Oct 13, 2015 at 03:40 PM
Here is my solution
using UnityEngine; using System.Collections;
public class TapDetector : MonoBehaviour {
public bool singleTap = false;
public bool doubleTap = false;
bool tapping = false;
float tapTime = 0;
float duration = .4f;
void Start()
{
}
void Update()
{
if (Input.GetMouseButtonDown(0))
{
if (tapping)
{
doubleTap = true;
Debug.Log("DoubleTap");
tapping = false;
}
else
{
tapping = true;
tapTime = duration;
}
}
if (tapping)
{
tapTime = tapTime - Time.deltaTime;
if (tapTime <= 0)
{
tapping = false;
singleTap = true;
Debug.Log("SingleTap");
}
}
}
void LateUpdate()
{
if (doubleTap) doubleTap = false;
if (singleTap) singleTap = false;
}
}
Answer by streeter12 · Nov 13, 2015 at 01:55 PM
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;
}
}
Your answer
Follow this Question
Related Questions
play animation when moving,and idle animation when stand still 1 Answer
Mid air "sprint" 2 Answers
Can the animation editor create local rotational data? 3 Answers
Enemy animtion and movement 0 Answers