タッチスクリーン咀Cheアルゴリズム

少し前、私の最初の個人用モバイルゲームがリリースされました。 一番下の行は、指で敵をかむ必要があるということです。 アルゴリズムは一意ではありませんが、まれです。 一見、2本の指のズーム動作を聞くだけで、複雑なことはないように見えますが、ゲームの開発中にさまざまな落とし穴が明らかになります。





すべてのコードは、2Dゲーム用のUnity3Dエンジン用にC#で記述されています。 コードに直接進みます。 Updateメソッドで、tachiの数を計算し、対応するアクションを実行します。 ワンタッチの場合、キャラクターを移動します:



//   if (Input.touchCount == 1) { //      ,      if (!compressing && !decompressing) { Touch singleTouch = Input.GetTouch(0); Vector3 targetPoint = Camera.main.ScreenToWorldPoint (singleTouch.position); targetPoint = new Vector3 (targetPoint.x, targetPoint.y, 0); transform.position = Vector3.MoveTowards (transform.position, targetPoint, movementSpeed * Time.deltaTime); } }
      
      





複雑なことは何もありません。先に進むことができます。 ツータッチ処理コード。 顎のクレンチング/アンクレンチングがない場合、キャラクターは2本の指の間を移動します。



 if (Input.touchCount > 1) { //    . Touch touch1 = Input.GetTouch(0); Touch touch2 = Input.GetTouch(1); //   ,     if (!compressing && !decompressing) { Vector3 targetPoint = Camera.main.ScreenToWorldPoint ((touch1.position + touch2.position) / 2); targetPoint = new Vector3 (targetPoint.x, targetPoint.y, 0); transform.position = Vector3.MoveTowards (transform.position, targetPoint, movementSpeed * Time.deltaTime); } float currentDistance = Vector2.Distance(touch1.position, touch2.position); if(pastFingersDistance == 0) { //  ,       pastFingersDistance = currentDistance; }else if(currentDistance < pastFingersDistance - fingersMunchDetectionMin) { //   .  ,   . SetCompression(); }else if(currentDistance > pastFingersDistance + fingersMunchDetectionMin) { //   .  ,   . SetDecompression(); } } // ,          . if(Input.touchCount < 2) pastFingersDistance = 0; //    ,    -   . if(Input.touchCount < 2 && isCompressed) SetDecompression();
      
      





fingersMunchDetectionMinは、 startを開始するのに十分な距離を決定する変数です。 数人の友人の助けを借りて十分に長く調整しました。 それぞれが異なる認識を持ち、その間に何かを推測しました。 テスト中、指で常に噛むことはユーザーにとって単に不便であることが明らかになりました。 簡単なタップで顎を圧縮する必要があり、上記の方法は次の形式を取得しました。



 if (Input.touchCount > 1) { //    . Touch touch1 = Input.GetTouch(0); Touch touch2 = Input.GetTouch(1); //     if (!compressing && !decompressing) { float touch1Time = 0; float touch2Time = 0; //     1 if (tapsHash.Contains (touch1.fingerId)) { float startTouch1Time = (float) tapsHash [touch1.fingerId]; touch1Time = Time.time - startTouch1Time; } //     2 if (tapsHash.Contains (touch2.fingerId)) { float startTouch2Time = (float) tapsHash [touch2.fingerId]; touch2Time = Time.time - startTouch2Time; } //         ,    . if (touch1Time > SECONDS_FOR_TAP && touch2Time > SECONDS_FOR_TAP) { Vector3 targetPoint = Camera.main.ScreenToWorldPoint ((touch1.position + touch2.position) / 2); targetPoint = new Vector3 (targetPoint.x, targetPoint.y, 0); transform.position = Vector3.MoveTowards (transform.position, targetPoint, movementSpeed * Time.deltaTime); } } float currentDistance = Vector2.Distance(touch1.position, touch2.position); if(pastFingersDistance == 0) { //  ,       pastFingersDistance = currentDistance; }else if(currentDistance < pastFingersDistance - fingersMunchDetectionMin) { //   .  ,   . SetCompression(); }else if(currentDistance > pastFingersDistance + fingersMunchDetectionMin) { //   .  ,   . SetDecompression(); } } // ,          . if(Input.touchCount < 2) pastFingersDistance = 0; //    ,    -   . if(Input.touchCount < 2 && isCompressed) SetDecompression(); //       . SetTapAttackListener ();
      
      





定数SECONDS_FOR_TAP-タップに割り当てられた時間と噛む距離がテストされ、長時間調整されました。 さて、実際には単純なタプを噛む最後の方法:



 void SetTapAttackListener() { if (Input.touchCount > 0) { foreach (Touch touch in Input.touches) { //   DetectOneTouchTap (touch); } } } void DetectOneTouchTap(Touch touch) { if (touch.phase == TouchPhase.Began) { //     ,    -  . // -  ,  -  . tapsHash.Add (touch.fingerId, Time.time); } else if(touch.phase == TouchPhase.Ended) { float startTouchTime = (float) tapsHash [touch.fingerId]; float timeOfTouch = Time.time - startTouchTime; //    ,     if (timeOfTouch <= SECONDS_FOR_TAP) { SetCompression(); SetDecompression(); } tapsHash.Remove (touch.fingerId); } }
      
      





最初は、コピーアンドペーストではなく、インターネット上でこのアルゴリズムを見つけようとしましたが、私の思考の流れをテストしました。 しかし、何も見つからなかったので、同僚を助けるためにそれをレイアウトすることにしました。 今では、コードが多少混chaとしていることがよくわかりますが、そうでない場合はコメントを待っています。



更新1:

アルゴリズムのデモンストレーション:





アップデート2:

ゲーム開発の記事



All Articles