0

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);
    }


}
  • You mean virtual joystick? – Programmer Apr 11 '18 at 14:15
  • Is there a way? : Yes there is. How to use onscreen buttons? : provide your code for reference so a man can help you. – Umair M Apr 11 '18 at 14:28
  • onscreen buttons are UI Buttons I suppouse? Then throw raycast from your camera, and add to the Onclick functions of the button your movement controller. It's hard to be more accurate on the response if you do not provide more code :( – Lotan Apr 11 '18 at 15:39
  • I have added the user input code I have and one of the move methods to show what I have going on with them. Thanks in advanced for any help. – Tony Williams Apr 12 '18 at 02:04

0 Answers0