I am making a Tetris clone for android with Unity 2017 using C#. I followed a Youtube tutorial and have the project basically finished. I would however like to move the spawned Tetrominos with onscreen buttons instead of using touch controls. Is there a way I can use the buttons to access the script on the currentActiveTetrimino to control its movement?
This code is attached to all the tetrominos, they are are randomly spawned and I am not sure how to get the buttons to be able to access this so I can move them.
void CheckUserInput () {
if (Input.GetKeyUp (KeyCode.RightArrow) || Input.GetKeyUp (KeyCode.LeftArrow)) {
movedImmediateHorizontal = false;
horizontalTimer = 0;
buttonDownWaitTimerHorizontal = 0;
}
if (Input.GetKeyUp (KeyCode.DownArrow)) {
movedImmediateVertical = false;
verticalTimer = 0;
buttonDownWaitTimerVertical = 0;
}
if (Input.GetKey (KeyCode.RightArrow)) {
MoveRight ();
}
if (Input.GetKey (KeyCode.LeftArrow)) {
MoveLeft ();
}
if (Input.GetKeyDown (KeyCode.UpArrow)) {
Rotate ();
}
if (Input.GetKey (KeyCode.DownArrow) || Time.time - fall >= fallSpeed) {
MoveDown ();
}
if (Input.GetKeyUp (KeyCode.Space)) {
SlamDown ();
}
}
public void MoveLeft () {
if (movedImmediateHorizontal) {
if (buttonDownWaitTimerHorizontal < buttonDownWaitMax) {
buttonDownWaitTimerHorizontal += Time.deltaTime;
return;
}
if (horizontalTimer < continuousHorizontalSpeed) {
horizontalTimer += Time.deltaTime;
return;
}
}
if (!movedImmediateHorizontal) {
movedImmediateHorizontal = true;
}
horizontalTimer = 0;
transform.position += new Vector3 (-1, 0, 0);
if (CheckIsValidPosition ()) {
FindObjectOfType<Game> ().UpdateGrid (this);
} else {
transform.position += new Vector3 (1, 0, 0);
}
}