やりたいこと
フリックして3Dのオブジェクトをフリック方向に合わせて90度回転させたいです。
課題
フリックしてオブジェクトが反転したときに動作が逆になってしまいます。
下にフリックしてオブジェクトが下を向いたときに、右にフリックすると右方向ではなく左方向を向いてしまったり、それらが重なると下にフリックしたのに右に回転したりしてしまいます。
現状のコード
C#
1using UnityEngine; 2using DG.Tweening; 3 4public class CubeMover : MonoBehaviour 5{ 6 GameObject Cube; 7 Vector2 _direction; 8 Vector3 _rotation; 9 string Direction; 10 bool _canmove = true; 11 bool _ismove = false; 12 13 // Start is called before the first frame update 14 void Start() 15 { 16 Cube = GameObject.FindGameObjectWithTag("Cube"); 17 } 18 19 void Update() 20 { 21 if (Input.touchCount > 0) 22 { 23 Touch touch = Input.GetTouch(0); 24 25 if (Input.GetTouch(0).phase == TouchPhase.Began) 26 { 27 _direction = Vector2.zero; 28 _rotation = Cube.transform.localEulerAngles; 29 30 if(!_ismove){ 31 _canmove = true; 32 } 33 } 34 35 if (Input.GetTouch(0).phase == TouchPhase.Moved) 36 { 37 _direction = touch.deltaPosition; 38 if (_direction.magnitude < 30.0f){ 39 return; 40 }else{ 41 float x = _direction.x; 42 float y = _direction.y; 43 44 float abs_x = Mathf.Abs(_direction.x); 45 float abs_y = Mathf.Abs(_direction.y); 46 47 if(abs_x > abs_y) 48 { 49 if(x > 0) 50 { 51 Direction = "right"; 52 } 53 else 54 { 55 Direction = "left"; 56 } 57 } 58 else 59 { 60 if(y > 0) 61 { 62 Direction = "up"; 63 } 64 else 65 { 66 Direction = "down"; 67 } 68 } 69 70 if(_canmove) 71 { 72 _canmove = false; 73 _ismove = true; 74 Move(); 75 } 76 } 77 } 78 if (Input.GetTouch(0).phase == TouchPhase.Ended) 79 { 80 _direction = Vector2.zero; 81 } 82 } 83 } 84 85 void Move() 86 { 87 float duration = 0.2f; 88 Vector3 _left = new Vector3(0,0,90); 89 Vector3 _right = new Vector3(0,0,-90); 90 Vector3 _up = new Vector3(90,0,0); 91 Vector3 _down = new Vector3(-90,0,0); 92 93 switch (Direction){ 94 case "up": 95 Cube.transform.DORotate(_rotation + _up , duration) 96 .OnComplete(() => { 97 _ismove = false; 98 }); 99 break; 100 101 case "down": 102 Cube.transform.DORotate(_rotation + _down , duration) 103 .OnComplete(() => { 104 _ismove = false; 105 }); 106 break; 107 108 case "right": 109 Cube.transform.DORotate(_rotation + _right , duration) 110 .OnComplete(() => { 111 _ismove = false; 112 }); 113 break; 114 115 case "left": 116 Cube.transform.DORotate(_rotation + _left , duration) 117 .OnComplete(() => { 118 _ismove = false; 119 }); 120 break; 121 } 122 } 123 124} 125
ご教授頂ければ幸いです。よろしくお願い致します。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/08/12 03:38