-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTetrisBlock Step 5 (Rotation).cs
More file actions
67 lines (58 loc) · 2.15 KB
/
TetrisBlock Step 5 (Rotation).cs
File metadata and controls
67 lines (58 loc) · 2.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TetrisBlock : MonoBehaviour //"TetrisBlock" MUST be renamed to whatever your script is called for the code to work!
{
public Vector3 rotationPoint;
private float previousTime;
public float fallTime = 0.8f;
public static int height = 20;
public static int width = 10;
//Start is called before the first frame update
void start()
{
}
// Update is called once per frame
void Update()
{
if(Input.GetKeyDown(KeyCode.LeftArrow))
{
transform.position += new Vector3(-1,0,0);
if(!ValidMove())
transform.position -= new Vector3(-1,0,0);
}
else if(Input.GetKeyDown(KeyCode.RightArrow))
{
transform.position += new Vector3(1,0,0);
if (!ValidMove())
transform.position -= new Vector3(1,0,0);
}
else if (Input.GetKeyDown(KeyCode.UpArrow))
{
//Rotation
transform.RotateAround(transform.TransformPoint(rotationPoint), new Vector3(0,0,1), -90); //You may need to make this -90
if (!ValidMove())
transform.RotateAround(transform.TransformPoint(rotationPoint), new Vector3(0, 0, 1), 90); //And this (+)90
}
if(Time.time - previousTime > (Input.GetKey(KeyCode.DownArrow) ? fallTime / 10 : fallTime))
{
transform.position += new Vector3(0, -1, 0);
if (!ValidMove())
transform.position -= new Vector3(0, -1, 0);
previousTime = Time.time;
}
}
bool ValidMove()
{
foreach (Transform children in transform)
{
int roundedX = Mathf.RoundToInt(children.transform.position.x);
int roundedY = Mathf.RoundToInt(children.transform.position.y);
if(roundedX < 0 || roundedX >= width || roundedY < 0 ||roundedY >= height)
{
return false;
}
}
return true;
}
}