I just started to program new controller for a character and i have huge problem. I want the code to detect double click from my left and right arrow keys, Its the first time i ever programed double click detection and i want to see if there is any better way for doing so (I'm sure there is). When the player pressed twice on the same arrow key it dash to that derection. I started with a really simple code but he just to long and i really need to optmize it. The double click detection part is betwenn 52 to 80. Code (csharp): using UnityEngine; using System.Collections; public class characterController : MonoBehaviour { public int dashSpeed = 20; private int movementSpeed = 10; //animatoer refernces private Animator anim; private AnimatorStateInfo animBaseState; private bool facingRight = true; //double click detection variables private bool sameKeyDetection = true; //Separating between left and right private bool firstKeyPressed = false; private float timeForNextKey = 0.15f; private float doubleClickTimer = -2; private float falseTimer = -2; static int dashState = Animator.StringToHash("Base Layer.Dash"); //flip the charcter sprite when pressing the negative derection. void Flip(){ facingRight = !facingRight; Vector3 theScale = transform.localScale; theScale.x *= -1; transform.localScale = theScale; } // Use this for initialization void Start () { anim = gameObject.GetComponent<Animator> (); } // Update is called once per frame void Update () { animBaseState = anim.GetCurrentAnimatorStateInfo (0); //input setting float velocity = Input.GetAxis ("Horizontal"); bool right = Input.GetButtonDown ("Right"); bool left = Input.GetButtonDown ("Left"); //animator parameters anim.SetFloat ("Speed", Mathf.Abs (velocity)); if (right firstKeyPressed sameKeyDetection){ anim.SetBool("Dash", true); firstKeyPressed = false; //if the player pressed RIGHT for rhe secound time falseTimer = Time.time; } else if(left firstKeyPressed !sameKeyDetection){ anim.SetBool("Dash", true); //if the player pressed LEFT for rhe secound time firstKeyPressed = false; falseTimer = Time.time; } else if (right){ sameKeyDetection = true; //if the player pressed RIGHT for the first time firstKeyPressed = true; doubleClickTimer = Time.time; } else if(left){ sameKeyDetection = false; ///if the player pressed LEFT for the first time firstKeyPressed = true; doubleClickTimer = Time.time; } if(Time.time - doubleClickTimer > timeForNextKey){ firstKeyPressed = false; //after 0.15 secound (timeForNextKey) it resets the input } // the player need to press twice on the same derection to //dash within 0.15 sec if(Time.time - falseTimer > 0.3){ // I added that timer because anim.SetBool("Dash", false); // the animator didnt responded to 1 fram input } //after detecting double click that part sould make the charcter dash //(there is simple movement in here as well) //dont have any porblems with that part. if (animBaseState.nameHash != dashState){ transform.Translate (Vector2.right * velocity * Time.deltaTime * movementSpeed); if (velocity > 0 !facingRight) Flip(); else if (velocity < 0 facingRight) Flip(); } else if (animBaseState.nameHash == dashState){ if (facingRight) transform.Translate(Vector2.right*dashSpeed*Time.deltaTime); else transform.Translate(-Vector2.right*dashSpeed*Time.deltaTime); } } } Hope you could help me find defrent approach to solve this problem. Thanks in advance
Your code isn't all that long tbh, but you could clean your code up a little by making a function to detect the double press. I must warn you that I have not tested this source code, but it *should* work. Let me know if you get stuck and I will try my best to help Utility class to make code inside your behaviour cleaner: This class should help you to detect double, triple, etc clicks Code (csharp): public static class MultiClickInput { // Default time in seconds for which to detect double clicking of a key. public const float DefaultTimeThreshold = 0.5f; private static string _multiClickAnchorKey; private static float _multiClickAnchorTime; private static int _multiClickCount; public static void CancelMultiClick() { _multiClickAnchorKey = null; } public static int GetMultiClickKeyCount(string key, float timeThreshold) { if (Input.GetButtonDown(key)) { // Do we need to cancel the last multi-click operation for this key? if (_multiClickAnchorKey == key) if (Time.time - _multiClickAnchorTime > timeThreshold) CancelMultiClick(); _multiClickAnchorTime = Time.time; // Has button been pressed for first time? if (_multiClickAnchorKey != key) { _multiClickAnchorKey = key; _multiClickCount = 1; } else { // Okay, so this is a multi-click operation! ++_multiClickCount; } return _multiClickCount; } return 0; } public static int GetMultiClickKeyCount(string key) { return GetMultiClickKeyCount(key, DefaultTimeThreshold); } public static bool HasDoubleClickedKey(string key, float timeThreshold) { return GetMultiClickKeyCount(key, timeThreshold) == 2; } public static bool HasDoubleClickedKey(string key) { return HasDoubleClickedKey(key, DefaultTimeThreshold); } } Usage Example: I wasn't quite sure what you were using the `falseTimer` variable for, so I have left that out. Code (csharp): using UnityEngine; using System.Collections; public class characterController : MonoBehaviour { public int dashSpeed = 20; private int movementSpeed = 10; //animatoer refernces private Animator anim; private AnimatorStateInfo animBaseState; private bool facingRight = true; //double click detection variables private float timeForNextKey = 0.15f; static int dashState = Animator.StringToHash("Base Layer.Dash"); //flip the charcter sprite when pressing the negative derection. void Flip(){ facingRight = !facingRight; Vector3 theScale = transform.localScale; theScale.x *= -1; transform.localScale = theScale; } // Use this for initialization void Start () { anim = gameObject.GetComponent<Animator> (); } // Update is called once per frame void Update () { animBaseState = anim.GetCurrentAnimatorStateInfo (0); //input setting float velocity = Input.GetAxis ("Horizontal"); //animator parameters anim.SetFloat ("Speed", Mathf.Abs (velocity)); if (MultiClickInput.HasDoubleClickedKey("Right", timeForNextKey)) { anim.SetBool("Dash", true); } else if (MultiClickInput.HasDoubleClickedKey("Left", timeForNextKey)) { anim.SetBool("Dash", true); } //after detecting double click that part sould make the charcter dash //(there is simple movement in here as well) //dont have any porblems with that part. if (animBaseState.nameHash != dashState){ transform.Translate (Vector2.right * velocity * Time.deltaTime * movementSpeed); if (velocity > 0 !facingRight) Flip(); else if (velocity < 0 facingRight) Flip(); } else if (animBaseState.nameHash == dashState){ if (facingRight) transform.Translate(Vector2.right*dashSpeed*Time.deltaTime); else transform.Translate(-Vector2.right*dashSpeed*Time.deltaTime); } } } I hope that this helps for you