简体   繁体   English

如何区分连续点击和单次点击? Unity动画?

[英]How distinguish between consecutive tapping and single taps? Unity animation?

Alright, Ive posted everywhere about this but cannot find an answer. 好吧,艾夫(Eve)到处都贴了这个,但是找不到答案。 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. 我在Unity中制作了一个C#游戏,并且正在使用代码根据用户的点击在我的闲置动画和步行周期之间进行转换。

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. 我的问题是我无法区分连续点击和单次点击-我尝试过递增int并将其设置为零(当角色停止移动,计时器,其他所有东西时,但没有得到可靠的结果)。

Basically my character has 3 states that he needs to switch between depending on number of taps : 基本上,我的角色有3种状态需要根据点击次数进行切换:

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 空闲=不轻拍,因此他不移动(轻拍触发1个单位向前移动)单步= 1个轻拍,在左右连续走动之间交替交替=多次轻拍,因此他连续移动

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. 如果该计时器超过了他完成1步所需的时间,那么我将过渡到连续行走动画。 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: 和:

在此处输入图片说明

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? 这可能吗?

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. 等待第二次点击0.5至0.8秒是好的。 For detailed explanation and code go to this unity thread : https://forum.unity3d.com/threads/single-tap-double-tap-script.83794/ 有关详细说明和代码,请访问以下统一线程: https : //forum.unity3d.com/threads/single-tap-double-tap-script.83794/

If you want to avoid complications, I would recommend TouchKit from the brilliant Prime31 . 如果您想避免复杂的情况,我建议使用精巧Prime31中的TouchKit It's free, open source and just work :) 它是免费的,开源的,并且可以正常工作:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM