简体   繁体   English

Unity Android touch输入重置

[英]Unity Android touch Input reset

first, I'm sorry for my english ahead 首先,对不起我的英语水平
I want player to move when they input one touch and to use skill only when input two touches. 我希望玩家在输入一键时移动,而仅在输入两键时才使用技能。
Below code seems fine except when I input two touches then took off the ' first touch' and re-input touch, it goes wrong. 下面的代码看起来不错,除了当我输入两次触摸然后取消“ 第一次触摸”并重新输入触摸时,它出了错。
but if I took off the strong text second touch it works fine. 但是,如果我第二次触摸强文本 ,效果很好。

    public class Test : MonoBehaviour {

        private void Update() {
            if(Input.touchCount > 0) {Movement();}
            if(Input.touchCount > 1) {Skill();}
        }
        private void Movement() {

            Vector2 inputPos = Camera.main.ScreenToWorldPoint(Input.GetTouch(0).position);
            transform.position = Vector3.MoveTowards(transform.position, inputPos, 30f * Time.deltaTime);
        }
        private void Skill() {print("skill fired");}

Working as Intended
//  [1]: https://imgur.com/ngjoabY
//  [2]: https://imgur.com/kkDAGEW



Problem the second reinput should fire skill instead of moving toward that
//  [3]: https://imgur.com/gD0x8rP
//  [4]: https://imgur.com/o06mJgs
    }

after you give it two touches, and took off the first one, second touch becomes the first one and it is caught in if(Input.touchCount > 0) {Movement();}. 在进行两次触摸并脱下第一个触摸后,第二个触摸变为第一个触摸,并且被if(Input.touchCount> 0){Movement();}捕获。
So in this stage, if you input again that should be GetTouch(1) right? 因此,在此阶段,如果再次输入,那应该是GetTouch(1)吗?
but that re-input second touch is become GetTouch(0)!! 但是重新输入的第二触摸变为GetTouch(0)!
So object that is supposed to use skill, moves to the second input position I've done everything I could but I couldn't find a solution. 因此,应该使用技能的对象移至第二个输入位置,我已尽力而为,但找不到解决方案。
I was thinking if I could flush the buffer for Input Touches, I can do this but it has just Get methods. 我在想是否可以刷新输入触摸的缓冲区,我可以这样做,但是它只有Get方法。
And Input.simulateMouseWithTouches It didn't work. 和Input.simulateMouseWithTouches无效。
Touch class variables can't even be compared with null. 触摸类变量甚至不能与null进行比较。

is it necessary to get touch counts? 是否需要获取触摸计数?
you can use crossplatforminput. 您可以使用crossplatforminput。 To import CrossPlatForm Assets->Import->CrossPlatFormInput. 要导入CrossPlatForm资产->导入-> CrossPlatFormInput。

after Importing Go To Standard Assets->CrossPlatformInput->Prefabs->5 Different prefabs ,choose that suits your project better. 导入后,转到“标准资产”->“ CrossPlatformInput”->“预制件”-> 5种不同的预制件,选择更适合您的项目的预制件。

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

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