How distinguish between consecutive tapping and single taps? Unity animation?

635 views Asked by At

Alright, Ive posted everywhere about this but cannot find an answer. I have a C# game being made in Unity and I am using code to transition between my idle animation to my walk cycle depending on users taps.

My problem is I cant distinguish between consecutive and single taps - I have tried incrementing ints and setting them to zero when the character stops moving, timers, everything but don't get reliable results.

Basically my character has 3 states that he needs to switch between depending on number of taps :

Idle = no tapping, so he's not moving (taps trigger 1 unit forward) single step = 1 tap, alternates between either right or left continuous walking = multiple taps, so he's moving continuously

So far Ive done this: Based on if his current pos is not his last (he is moving) I have a timer that records his time walking. If this timer is above about the time it takes for him to complete 1 step, I transition to my continuous walk anim. If he stops moving thus timer is set to zero.

Reason I need a single step state (and this state works well) is I have to alternate between right and left not just play a portion of the full cycle, this is why I have an offset that moves through the full anim in increments.

if (currentPos != lastPos && didCollide == false) {
            //print ("moving now");
            animator.SetBool ("isWalking", true);
            if (timeWalking >= 0.1f) {
                //this is for continuous cycle
                animator.SetBool("contWalking", true);
            } else {
                if (InputManager.stepCount % 2 != 0) {
                    //individual cycle left
                    animator.SetFloat ("walkOffset",animOffset);

                } else {
                    //individual cycle right
                    animator.SetFloat ("walkOffset",animOffset);

                }
                animOffset += 0.1f;
            }

            timeWalking += Time.deltaTime;

        } else {
            //stopped
            animator.SetBool ("isWalking", false);
            animator.SetBool("contWalking", false);
            timeWalking = 0.0f;
        }
        lastPos = currentPos;

And:

enter image description here

Problem is the time seems to vary, and this can get him to continuous cycle only rarely, and then it is glitchy. There is a lot of stuttering as it mainly just goes to the single step state and cant get to continuous even when he's been moving for a while.

I am out of ideas. How can I transition to a continuous walk cycle based on if tapping is consecutive? Don't think timers are the way to go. Is this possible?

2

There are 2 answers

0
Adil G On

What you need to do is save the time of first tap and wait for the second tap. If second tap happens run the double tap state, else run the single tap state. The wait of 0.5 to 0.8 secs for 2nd tap is good. For detailed explanation and code go to this unity thread : https://forum.unity3d.com/threads/single-tap-double-tap-script.83794/

0
Jerome Maurey-Delaunay On

If you want to avoid complications, I would recommend TouchKit from the brilliant Prime31. It's free, open source and just work :)