Search Unity

Instant piece drop

Discussion in 'Scripting' started by JacksonTheXtremeGamer, Jul 18, 2019.

  1. JacksonTheXtremeGamer

    JacksonTheXtremeGamer

    Joined:
    Jun 15, 2019
    Posts:
    108
    Right here is the code used for moving the pieces around. But I want to try a different approach for my game. Here's what I have:
    Code (CSharp):
    1. public class Piece : MonoBehaviour
    2. {
    3.     float fall = 0;
    4.     public float fallSpeed = 1;
    5.     //Use this for initialization
    6.     void Start()
    7.     {
    8.        
    9.     }
    10.  
    11.     // Update is called once per frame
    12.     void Update()
    13.     {
    14.         CheckUserInput();
    15.     }
    16.     void CheckUserInput()
    17.     {
    18.         if (Input.GetKeyDown(KeyCode.RightArrow))
    19.         {
    20.             transform.position += new Vector3(1, 0, 0);
    21.             if (CheckIsValidPosition())
    22.             {
    23.                 FindObjectOfType<Board>().UpdateGrid(this);
    24.             }
    25.             else
    26.             {
    27.                 transform.position += new Vector3(-1, 0, 0);
    28.             }
    29.         }
    30.         else if (Input.GetKeyDown(KeyCode.LeftArrow))
    31.         {
    32.             transform.position += new Vector3(-1, 0, 0);
    33.             if (CheckIsValidPosition())
    34.             {
    35.                 FindObjectOfType<Board>().UpdateGrid(this);
    36.             }
    37.             else
    38.             {
    39.                 transform.position += new Vector3(1, 0, 0);
    40.             }
    41.         }
    42.         else if (Input.GetKeyDown(KeyCode.DownArrow) || Time.time - fall >= fallSpeed)
    43.         {
    44.             transform.position += new Vector3(0, -1, 0);
    45.             if (CheckIsValidPosition())
    46.             {
    47.                 FindObjectOfType<Board>().UpdateGrid(this);
    48.             }
    49.             else
    50.             {
    51.                 transform.position += new Vector3(0, 1, 0);
    52.                 enabled = false;
    53.                 FindObjectOfType<Board>().SpawnNextPiece();
    54.             }
    55.             fall = Time.time;
    56.         }
    57.     }
    58.     bool CheckIsValidPosition()
    59.     {
    60.         foreach (Transform unit in transform)
    61.         {
    62.             Vector2 pos = FindObjectOfType<Board>().Round (unit.position);
    63.  
    64.             if (FindObjectOfType<Board>().CheckIsInsideGrid (pos) == false)
    65.             {
    66.                 return false;
    67.             }
    68.             if (FindObjectOfType<Board>().GetTransformAtGridPosition(pos) != null && FindObjectOfType<Board>().GetTransformAtGridPosition(pos).parent != transform)
    69.                 {
    70.                 return false;
    71.             }
    72.         }
    73.         return true;
    74.     }
    75. }
    I want to make it so that the pieces drop instantly to the bottom rather than slowly moving down. Any ideas?
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,742
    When you decide to go down one line (line 42), instead of doing it, just set a variable, maybe like so:

    Code (csharp):
    1. int LinesToGoDown = 0;
    2.  
    3. // left/right parts
    4.  
    5. else if (Input.GetKeyDown(KeyCode.DownArrow) || Time.time - fall >= fallSpeed)
    6. {
    7.  LinesToGoDown = 1;
    8. }
    9. else if (?WhateverIsAllTheWayDownKey?)
    10. {
    11.  LinesToGoDown = 1000; // something big
    12. }
    Now finally do a for loop for the LinesToGoDown count, and do the "move down one line" code that was formerly in the "else if" block.

    ALSO, make that block terminate early if the block contacts the ones below it. Effectively the 1-down code will run as before, but the 1000-down code will (all in one frame) move the block down line by line, checking for it to hit.
     
    JacksonTheXtremeGamer likes this.
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,742
    You're very welcome!
     
  4. JacksonTheXtremeGamer

    JacksonTheXtremeGamer

    Joined:
    Jun 15, 2019
    Posts:
    108
    I want to use only the down button for the insta-drop for the pieces to replace the other mechanic. And about this loop, how should I build this without errors popping up in my face? I have a bad habit of that happening.