質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Unity3D

Unity3Dは、ゲームや対話式の3Dアプリケーション、トレーニングシュミレーション、そして医学的・建築学的な技術を可視化する、商業用の開発プラットフォームです。

Unity

Unityは、Unity Technologiesが開発・販売している、IDEを内蔵するゲームエンジンです。主にC#を用いたプログラミングでコンテンツの開発が可能です。

Q&A

解決済

1回答

2390閲覧

スワイプでオブジェクトを斜めに移動させたい

futonmin

総合スコア33

Unity3D

Unity3Dは、ゲームや対話式の3Dアプリケーション、トレーニングシュミレーション、そして医学的・建築学的な技術を可視化する、商業用の開発プラットフォームです。

Unity

Unityは、Unity Technologiesが開発・販売している、IDEを内蔵するゲームエンジンです。主にC#を用いたプログラミングでコンテンツの開発が可能です。

0グッド

0クリップ

投稿2020/10/12 12:12

前提・実現したいこと

スワイプ機能を実装したが、プレイヤーを斜めに動かすやり方がわからないので教えてほしい

該当のソースコード

C#

1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4using UnityEngine.EventSystems; 5using UnityChan; 6 7//スワイプ機能 8public class ScreenInput : MonoBehaviour 9{ 10 // スワイプ最小移動距離 11 [SerializeField] 12 private Vector2 SwipeMinRange = new Vector2(10.0f, 10.0f); 13 // TAPをNONEに戻すまでのカウント 14 [SerializeField] 15 private int NoneCountMax = 2; 16 private int NoneCountNow = 0; 17 // スワイプ入力距離 18 private Vector2 SwipeRange; 19 // 入力方向記録用 20 private Vector2 InputSTART; 21 private Vector2 InputMOVE; 22 23 // スワイプの方向 24 public enum SwipeDirection 25 { 26 NONE, 27 TAP, 28 UP, 29 RIGHT, 30 DOWN, 31 LEFT, 32 } 33 private SwipeDirection NowSwipe = SwipeDirection.NONE; 34 35 36 // Update is called once per frame 37 void Update() 38 { 39 40 GetInputVector(); 41 } 42 43 // 入力の取得 44 private void GetInputVector() 45 { 46 // Unity上での操作取得 47 if (Application.isEditor) 48 { 49 if (Input.GetMouseButtonDown(0)) 50 { 51 InputSTART = Input.mousePosition; 52 } 53 else if (Input.GetMouseButton(0)) 54 { 55 InputMOVE = Input.mousePosition; 56 SwipeCLC(); 57 } 58 else if (NowSwipe != SwipeDirection.NONE) 59 { 60 ResetParameter(); 61 } 62 } 63 // 端末上での操作取得 64 else 65 { 66 if (Input.touchCount > 0) 67 { 68 Touch touch = Input.touches[0]; 69 if (touch.phase == TouchPhase.Began) 70 { 71 InputSTART = touch.position; 72 } 73 else if (touch.phase == TouchPhase.Moved) 74 { 75 InputMOVE = Input.mousePosition; 76 SwipeCLC(); 77 } 78 } 79 else if (NowSwipe != SwipeDirection.NONE) 80 { 81 ResetParameter(); 82 } 83 } 84 } 85 86 // 入力内容からスワイプ方向を計算 87 private void SwipeCLC() 88 { 89 SwipeRange = new Vector2((new Vector3(InputMOVE.x, 0, 0) - new Vector3(InputSTART.x, 0, 0)).magnitude, (new Vector3(0, InputMOVE.y, 0) - new Vector3(0, InputSTART.y, 0)).magnitude); 90 91 if (SwipeRange.x <= SwipeMinRange.x && SwipeRange.y <= SwipeMinRange.y) 92 { 93 NowSwipe = SwipeDirection.TAP; 94 } 95 else if (SwipeRange.x > SwipeRange.y) 96 { 97 float _x = Mathf.Sign(InputMOVE.x - InputSTART.x); 98 if (_x > 0) NowSwipe = SwipeDirection.RIGHT; 99 else if (_x < 0) NowSwipe = SwipeDirection.LEFT; 100 } 101 else 102 { 103 float _y = Mathf.Sign(InputMOVE.y - InputSTART.y); 104 if (_y > 0) NowSwipe = SwipeDirection.UP; 105 else if (_y < 0) NowSwipe = SwipeDirection.DOWN; 106 } 107 } 108 109 // NONEにリセット 110 private void ResetParameter() 111 { 112 NoneCountNow++; 113 if (NoneCountNow >= NoneCountMax) 114 { 115 NoneCountNow = 0; 116 NowSwipe = SwipeDirection.NONE; 117 SwipeRange = new Vector2(0, 0); 118 } 119 } 120 121 // スワイプ方向の取得 122 public SwipeDirection GetNowSwipe() 123 { 124 return NowSwipe; 125 } 126 127 // スワイプ量の取得 128 public float GetSwipeRange() 129 { 130 if (SwipeRange.x > SwipeRange.y) 131 { 132 return SwipeRange.x; 133 } 134 else 135 { 136 return SwipeRange.y; 137 } 138 } 139 140 // スワイプ量の取得 141 public Vector2 GetSwipeRangeVec() 142 { 143 if (NowSwipe != SwipeDirection.NONE) 144 { 145 return new Vector2(InputMOVE.x - InputSTART.x, InputMOVE.y - InputSTART.y); 146 } 147 else 148 { 149 return new Vector2(0, 0); 150 } 151 } 152}

C#

1// 2// Mecanimのアニメーションデータが、原点で移動しない場合の Rigidbody付きコントローラ 3// サンプル 4// 2014/03/13 N.Kobyasahi 5// 6using UnityEngine; 7using System.Collections; 8using System.Runtime.CompilerServices; 9using System.Collections.Generic; 10using System.Collections.Specialized; 11using System; 12using UnityEngine.UI; 13 14//プレイヤー情報 15namespace UnityChan 16{ 17// 必要なコンポーネントの列記 18 [RequireComponent(typeof(Animator))] 19 [RequireComponent(typeof(CapsuleCollider))] 20 [RequireComponent(typeof(Rigidbody))] 21 22 public class UnityChanControlScriptWithRgidBody : MonoBehaviour 23 { 24 public float animSpeed = 1.5f; // アニメーション再生速度設定 25 public float lookSmoother = 3.0f; // a smoothing setting for camera motion 26 public bool useCurves; // Mecanimでカーブ調整を使うか設定する 27 // このスイッチが入っていないとカーブは使われない 28 public float useCurvesHeight = 0.5f; // カーブ補正の有効高さ(地面をすり抜けやすい時には大きくする) 29 30 // 以下キャラクターコントローラ用パラメタ 31 // 前進速度 32 public float PlayerSpeed = 0.05f; 33 // 後退速度 34 //public float backwardSpeed = 2.0f; 35 // 旋回速度 36 public float rotateSpeed = 0.2f; 37 // ジャンプ威力 38 public float jumpPower = 3.0f; 39 public float h; 40 public float v; 41 private Vector3 Player_pos; //プレイヤーのポジション 42 43 // 移動速度の入力に対する追従度 44 public float moveForceMultiplier; 45 //カメラ取得 46 public GameObject mainCamera; 47 // カメラの向きに合わせて移動させたい場合はtrue(慣性) 48 public bool isUseCameraDirection = true; 49 50 //射出するオブジェクト 51 [SerializeField] 52 private GameObject throwingObject; 53 //射出角度 54 [SerializeField] 55 private float throwingAngle; 56 57 // キャラクターコントローラ(カプセルコライダ)の参照 58 private CapsuleCollider col; 59 private Rigidbody rb; 60 //public Rigidbody rb; //自分で書き換えた 61 // キャラクターコントローラ(カプセルコライダ)の移動量 62 private Vector3 velocity; 63 // CapsuleColliderで設定されているコライダのHeiht、Centerの初期値を収める変数 64 private float orgColHight; 65 private Vector3 orgVectColCenter; 66 private Animator anim; // キャラにアタッチされるアニメーターへの参照 67 private AnimatorStateInfo currentBaseState; // base layerで使われる、アニメーターの現在の状態の参照 68 69 // アニメーター各ステートへの参照 70 static int idleState = Animator.StringToHash ("Base Layer.Idle"); 71 static int locoState = Animator.StringToHash ("Base Layer.Locomotion"); 72 static int jumpState = Animator.StringToHash ("Base Layer.Jump"); 73 static int restState = Animator.StringToHash ("Base Layer.Rest"); 74 75 //スワイプの情報 76 [SerializeField] 77 ScreenInput Input; 78 [SerializeField] 79 private GameObject playerObject; 80 81 // 初期化 82 void Start () 83 { 84 Application.targetFrameRate = 60; // 30FPSに設定 85 Player_pos = GetComponent<Transform>().position; //最初の時点でのプレイヤーのポジションを取得 86 // Animatorコンポーネントを取得する 87 anim = GetComponent<Animator>(); 88 // CapsuleColliderコンポーネントを取得する(カプセル型コリジョン) 89 col = GetComponent<CapsuleCollider>(); 90 rb = GetComponent<Rigidbody>(); 91 // CapsuleColliderコンポーネントのHeight、Centerの初期値を保存する 92 orgColHight = col.height; 93 orgVectColCenter = col.center; 94 } 95 96 void Update() 97 { 98       //_workにスワイプ量を代入 99 float _work = Input.GetSwipeRange(); 100 //_workにプレイヤーのスピードを代入 101       //スピードを一定にするため 102 _work = PlayerSpeed; 103 104 //スワイプ 105 switch (Input.GetNowSwipe()) 106 { 107 case ScreenInput.SwipeDirection.UP: //前に進む 108 transform.localPosition = new Vector3(transform.localPosition.x, transform.localPosition.y, transform.localPosition.z - _work); 109 break; 110 case ScreenInput.SwipeDirection.DOWN: //後ろに進む 111 transform.localPosition = new Vector3(transform.localPosition.x, transform.localPosition.y, transform.localPosition.z + _work); 112 break; 113 case ScreenInput.SwipeDirection.LEFT: //左に進む 114 transform.localPosition = new Vector3(transform.localPosition.x + _work, transform.localPosition.y, transform.localPosition.z); 115 break; 116 case ScreenInput.SwipeDirection.RIGHT: //右に進む 117 transform.localPosition = new Vector3(transform.localPosition.x - _work, transform.localPosition.y, transform.localPosition.z); 118 break; 119 } 120 } 121 122 // キャラクターのコライダーサイズのリセット関数 123 void resetCollider () 124 { 125 // コンポーネントのHeight、Centerの初期値を戻す 126 col.height = orgColHight; 127 col.center = orgVectColCenter; 128 } 129 } 130}

試したこと

https://deve-cat.com/unity-flick-swipe/
上のサイトを参考にスワイプ機能を実装した

補足情報(FW/ツールのバージョンなど)

環境:Windows10、VisualStudio2019、Unity2020.1.6f1 Personal

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

【Unity】アナログスティックの入力値を8方向にスナップする - さけのさかなのブログなんて記事が見つかりましたが、これと同様の手が使えないでしょうかね?

SwipeDirectionに方角を追加し、ついでに各方角を指す単位ベクトルを用意し...

C#

1 // スワイプの方向 2 // 方角はRIGHTを起点に反時計回りに定義し、それらが連番になるようにしておく 3 public enum SwipeDirection 4 { 5 NONE, 6 TAP, 7 RIGHT, 8 UPRIGHT, 9 UP, 10 UPLEFT, 11 LEFT, 12 DOWNLEFT, 13 DOWN, 14 DOWNRIGHT, 15 } 16 17 public static readonly Vector2[] DirectionVectors = { 18 Vector2.right, 19 (Vector2.right + Vector2.up).normalized, 20 Vector2.up, 21 (Vector2.up + Vector2.left).normalized, 22 Vector2.left, 23 (Vector2.left + Vector2.down).normalized, 24 Vector2.down, 25 (Vector2.down + Vector2.right).normalized 26 };

SwipeCLCに変更を加え...

C#

1 // 入力内容からスワイプ方向を計算 2 private void SwipeCLC() 3 { 4 SwipeRange = new Vector2((new Vector3(InputMOVE.x, 0, 0) - new Vector3(InputSTART.x, 0, 0)).magnitude, (new Vector3(0, InputMOVE.y, 0) - new Vector3(0, InputSTART.y, 0)).magnitude); 5 6 if (SwipeRange.x <= SwipeMinRange.x && SwipeRange.y <= SwipeMinRange.y) 7 { 8 NowSwipe = SwipeDirection.TAP; 9 } 10 else 11 { 12 // 入力方向を8方向にスナップし、対応するSwipeDirectionを得る 13 Vector2 swipe = InputMOVE - InputSTART; 14 int directionIndex = (Mathf.RoundToInt(4.0f * Mathf.Atan2(swipe.y, swipe.x) / Mathf.PI) + 8) % 8; 15 NowSwipe = (SwipeDirection)System.Enum.ToObject(typeof(SwipeDirection), (int)SwipeDirection.RIGHT + directionIndex); 16 } 17 }

GetSwipeRangeにも変更を加え...

C#

1 // スワイプ量の取得 2 public float GetSwipeRange() 3 { 4 // SwipeRangeを8方向(実質的には左・左上・上の3方向)にスナップし... 5 int directionIndex = (Mathf.RoundToInt(4.0f * Mathf.Atan2(SwipeRange.y, SwipeRange.x) / Mathf.PI) + 8) % 8; 6 if ((directionIndex % 2) == 1) 7 { 8 // 斜め入力なら斜め成分を抽出して返し... 9 return Vector2.Dot(SwipeRange, Vector2.one.normalized); 10 } 11 else 12 { 13 // 水平・垂直入力ならX、Yのうち大きい方を返す 14 return Mathf.Max(SwipeRange.x, SwipeRange.y); 15 } 16 }

UnityChanControlScriptWithRgidBodyUpdateは下記のようにしてみてはいかがでしょう。

C#

1 void Update() 2 { 3 //_workにスワイプ量を代入 4 // ※この直後に_workをPlayerSpeedで上書きしてしまうなら、このGetSwipeRangeは不要なんじゃないですかね? 5 float _work = Input.GetSwipeRange(); 6 //_workにプレイヤーのスピードを代入 7 //スピードを一定にするため 8 _work = PlayerSpeed; 9 10 //スワイプ 11 // GetNowSwipeの結果をもとにScreenInput.DirectionVectorsから方角を引いてきて、それを使って移動させる 12 ScreenInput.SwipeDirection swipe = Input.GetNowSwipe(); 13 int directionIndex = (int)swipe - (int)ScreenInput.SwipeDirection.RIGHT; 14 Vector2 directionVector = (directionIndex >= 0) ? ScreenInput.DirectionVectors[directionIndex] : Vector2.zero; 15 transform.localPosition += new Vector3(directionVector.x * _work, 0.0f, -directionVector.y * _work); 16 }

投稿2020/10/15 14:29

編集2020/10/15 14:35
Bongo

総合スコア10807

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

futonmin

2020/10/15 17:52

回答ありがとうございます。 非常に分かりやすかったです。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問