I’m making a Tetris game, but i’m still stucked at moving code by touch or swipe :(, so please help me how to fix it
void Update() {
if (State != TetrimoState.Fixed && State != TetrimoState.Preview && State != TetrimoState.Spawning) {
if (State != TetrimoState.Landed && Input.GetAxis("Vertical") < 0)
StartCoroutine(FallingDown());
if (Input.GetButtonDown("Horizontal") )
StartCoroutine(MoveHorizontal());
if ((Input.GetKeyDown(KeyCode.UpArrow) && Time.time > NextSwap)) {
if (this.CanRotate) {
StartCoroutine(RotateTetrimo());
NextSwap = Time.time + SwapCooldown;
}
}
// Automatic falling down
if (NextFall < 0) {
StartCoroutine(FallingDown());
NextFall = FallingCooldown;
}
NextFall -= FallingSpeed * Time.deltaTime;
}
}
here the move right, left and down
IEnumerator MoveHorizontal() {
IsMovingHorizontal = true;
float moved = 0.0f;
float direction = Input.GetAxis("Horizontal");
if ((this.CanMoveRight && direction > 0) || (this.CanMoveLeft && direction < 0)) {
while (moved <= 1.0f) {
float moveStep = Mathf.Min(HorizontalSpeed * Time.deltaTime, 1.1f - moved); // 1.1f since float has some rounding problems!
if (direction > 0)
transform.Translate(Vector3.right * moveStep, Space.World);
else if (direction < 0)
transform.Translate(Vector3.left * moveStep, Space.World);
moved += moveStep;
yield return 0;
}
// We will correct the actual position of each stone when it landed
transform.position = new Vector2(Mathf.RoundToInt(transform.position.x), Mathf.RoundToInt(transform.position.y));
}
IsMovingHorizontal = false;
}
IEnumerator FallingDown() {
if (this.CanMoveDown) {
transform.Translate(Vector3.down, Space.World);
}
else {
if (State == TetrimoState.Falling) {
// After the Tetrimo has landed, the player can move it in the first 400ms.
this.audio.Play();
State = TetrimoState.Landed;
yield return new WaitForSeconds(0.4f);
while (IsMovingHorizontal) // Wait for end of possible MoveHorizontal - calls
yield return new WaitForEndOfFrame();
if (this.CanMoveDown) {
State = TetrimoState.Falling;
}
else {
State = TetrimoState.Fixed;
foreach (Transform child in transform) {
Tetrimo.FieldMatrix[Mathf.RoundToInt(child.position.y), Mathf.RoundToInt(child.position.x)] = child.gameObject;
}
ArrayList lines = FindLines();
if (lines.Count > 0) {
int FourLineBonus = 0;
and the code to check
bool CanMoveDown {
get {
if(IsMovingHorizontal)
return false;
foreach (Transform child in transform) {
if (Mathf.RoundToInt(child.position.y - 1) < 0 || FieldMatrix[Mathf.RoundToInt(child.position.y - 1), Mathf.RoundToInt(child.position.x)] != null)
return false;
}
return true;
}
}
bool CanMoveRight {
get {
bool canMoveRight = true;
foreach (Transform child in transform) {
canMoveRight &= Mathf.RoundToInt(child.position.x + 1) <= Tetrimo.FieldSize.Right && FieldMatrix[Mathf.RoundToInt(child.position.y), Mathf.RoundToInt(child.position.x + 1)] == null;
}
return canMoveRight;
}
}
bool CanMoveLeft {
get {
bool canMoveLeft = true;
foreach (Transform child in transform) {
canMoveLeft &= Mathf.RoundToInt(child.position.x - 1) >= Tetrimo.FieldSize.Left && FieldMatrix[Mathf.RoundToInt(child.position.y), Mathf.RoundToInt(child.position.x - 1)] == null;
}
return canMoveLeft;
}
}
bool CanRotate {
get {
// Iterate through each TetrimoParts
for (int index = 0; index < Shapes.GetLength(2); index++) {
Vector2 tmp = new Vector2(transform.position.x, transform.position.y) + Shapes[ShapeIndex, (RotationIndex + 1) % Shapes.GetLength(2), index];
if (tmp.x < FieldSize.Left || tmp.x > FieldSize.Right || tmp.y < FieldSize.Bottom || tmp.y > FieldSize.Top)
return false;
if (FieldMatrix[Mathf.RoundToInt(tmp.y), Mathf.RoundToInt(tmp.x)] != null)
return false;
}
return true;
}
}
sorry for my English, please give me a hint how to do it (i don’t know how gettouch code make)