###前提・実現したいこと
Unityでテトリスを作っています。
その中でテトリスのミノ(ブロック)を動かすのに、キー入力でやっているのですが、そこで一つ問題が発生しました。(https://www.youtube.com/watch?v=Myy6xOi7Efg) こちらを参考にさせていただいて、キーを長押ししたら、その入力があるだけ動かすというものを作っていたのですが、左に移動させることはできるのですが、右および下移動ができなくなってしまいました。移動できないというよりかは、自由落下(時間経過で落ちていく)が起こっているときにしか動かせないという感じです。左移動の部分と右移動の部分で同じものを書いているはずなのですが、何が原因かわかりますか?
###該当のソースコード
C#
1using UnityEngine; 2using System.Collections; 3 4public class Group : MonoBehaviour { 5 // Time since last gravity tick 6 float lastFall = 0; 7 8 public float verticalTimer = 0; 9 public float horizontalTimer = 0; 10 11 public float continuosVerticalSpeed = 0.05f; 12 public float continuosHorizontalSpeed = 0.1f; 13 14 15 16 17 18 19 20 bool isValidGridPos() { 21 foreach (Transform child in transform) { 22 Vector2 v = Grid.roundVec2(child.position); 23 24 // Not inside Border? 25 if (!Grid.insideBorder(v)) 26 return false; 27 28 // Block in grid cell (and not part of same group)? 29 if (Grid.grid[(int)v.x, (int)v.y] != null && 30 Grid.grid[(int)v.x, (int)v.y].parent != transform) 31 return false; 32 } 33 return true; 34 } 35 void updateGrid() { 36 // Remove old children from grid 37 for (int y = 0; y < Grid.h; ++y) 38 for (int x = 0; x < Grid.w; ++x) 39 if (Grid.grid[x, y] != null) 40 if (Grid.grid[x, y].parent == transform) 41 Grid.grid[x, y] = null; 42 43 // Add new children to grid 44 foreach (Transform child in transform) { 45 Vector2 v = Grid.roundVec2(child.position); 46 Grid.grid[(int)v.x, (int)v.y] = child; 47 } 48 } 49 void Update() { 50 // Move Left 51 if (Input.GetKey(KeyCode.A)) { 52 53 if (horizontalTimer < continuosHorizontalSpeed) 54 { 55 horizontalTimer += Time.deltaTime; 56 return; 57 } 58 59 horizontalTimer = 0; 60 61 // Modify position 62 transform.position += new Vector3(-1, 0, 0); 63 64 // See if valid 65 if (isValidGridPos()) 66 // It's valid. Update grid. 67 updateGrid(); 68 else 69 // It's not valid. revert. 70 transform.position += new Vector3(1, 0, 0); 71 } 72 73 // Move Right 74 else if (Input.GetKeyDown(KeyCode.D)) { 75 if (horizontalTimer < continuosHorizontalSpeed) 76 { 77 horizontalTimer += Time.deltaTime; 78 return; 79 } 80 81 // Modify position 82 transform.position += new Vector3(1, 0, 0); 83 84 // See if valid 85 if (isValidGridPos()) 86 // It's valid. Update grid. 87 updateGrid(); 88 else 89 // It's not valid. revert. 90 transform.position += new Vector3(-1, 0, 0); 91 } 92 93 // Right Rotate 94 else if (Input.GetKeyDown(KeyCode.O)) { 95 transform.Rotate(0, 0, -90); 96 97 // See if valid 98 if (isValidGridPos()) 99 // It's valid. Update grid. 100 updateGrid(); 101 else 102 // It's not valid. revert. 103 transform.Rotate(0, 0, 90); 104 } 105 //Left Rotate 106 else if (Input.GetKeyDown(KeyCode.I)) 107 { 108 transform.Rotate(0 , 0, 90); 109 if (isValidGridPos()) 110 // It's valid. Update grid. 111 updateGrid(); 112 else 113 // It's not valid. revert. 114 transform.Rotate(0, 0, -90); 115 } 116 117 // Move Downwards and Fall 118 else if (Input.GetKeyDown(KeyCode.S) || 119 Time.time - lastFall >= 1) { 120 if(verticalTimer < continuosHorizontalSpeed) 121 { 122 verticalTimer += Time.deltaTime; 123 return; 124 } 125 verticalTimer = 0; 126 127 128 // Modify position 129 transform.position += new Vector3(0, -1, 0); 130 131 // See if valid 132 if (isValidGridPos()) { 133 // It's valid. Update grid. 134 updateGrid(); 135 } else { 136 // It's not valid. revert. 137 transform.position += new Vector3(0, 1, 0); 138 139 // Clear filled horizontal lines 140 Grid.deleteFullRows(); 141 142 // Spawn next Group 143 144 createRandObjs.Instance.spawnNext(); 145 146 // Disable script 147 enabled = false; 148 } 149 150 lastFall = Time.time; 151 } 152 } 153 154 // Use this for initialization 155 void Start () { 156 // Default position not valid? Then it's game over 157 if (!isValidGridPos()) { 158 Debug.Log("GAME OVER"); 159 Destroy(gameObject); 160 } 161 } 162 163 164 165 166}
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/10/08 09:25