Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by makergaming · Dec 14, 2017 at 05:23 AM · raycastgridvector3.distancegridmove

I'm almost finished with my game, but I have a major bug that makes it basically unplayable, can someone help please

I have a bug in my game that I have spent day and night trying to solve but with no success, so please help if you can. Basicaly I have modified the grid(see picture for clarification) and everything seems to work except when I place the pieces at the corner or the edge pieces it suddenly change shape to fit the grid. I am not that good at describing things so I have taken a few screenshots to illustrate.

When I start the game

When I try to set a piece in the grid but one piece is outside

Piece suddenly change shape, when it shouldn't fit and return to spawnpoint

enter image description here

enter image description here

enter image description here

Here is the code for that is attached to every tetris piece:

     public TetrimoPiece[] tetrimoPieces;
 public Vector3 originalScale;
 public Vector3 originalPos;
 public Vector3 originalSpawnPos;

 Vector3[] _minAndMaxPoints;
 Vector3 _centroid;

 public bool isBeingDragged;

 public TetrimoSpawnPoint assignedSpawnPoint;
 public int originalSortingOrder;

 public Color color;

 public int SortingOrder {
     
     get {
         return tetrimoPieces [0].SortingOrder;            
     }

     set {
         foreach (TetrimoPiece piece in tetrimoPieces) {
             piece.SortingOrder = value;
         }
     }

 }

 void Awake ()
 {
     tetrimoPieces = GetChildTetrimoPieces ();
     originalSortingOrder = SortingOrder;

     originalScale = transform.localScale;
     originalPos = transform.localPosition;
     transform.rotation = Quaternion.Euler(transform.rotation.eulerAngles.x, 0, 0);

     if (tetrimoPieces.Count () > 0) {
         List<Transform> tetrimoPiecesTransforms = (from tetrimoPiece in tetrimoPieces
                                                    select tetrimoPiece.transform).ToList ();
     
         _minAndMaxPoints = GetMinAndMaxPoints (tetrimoPiecesTransforms);
         _centroid = FindCentroid (tetrimoPiecesTransforms);
     }

     SetTetrimoColor ();

 }

 void Start ()
 {
 }


 public TetrimoPiece[] GetChildTetrimoPieces ()
 {
     return GetComponentsInChildren<TetrimoPiece> ();
 }

 public void SetTetrimoColor(){
     foreach (TetrimoPiece piece in tetrimoPieces) {
         piece.SetColor(color);
     }
 }

 public void GenerateCollider ()
 {

     List<Transform> tetrimoPiecesTransforms = new List<Transform> ();

     foreach (TetrimoPiece piece in tetrimoPieces) {
         tetrimoPiecesTransforms.Add (piece.transform);
     }

     Vector3[] minAndMaxPoints = GetMinAndMaxPoints (tetrimoPiecesTransforms);
     Vector3 minPoint = minAndMaxPoints [0];
     Vector3 maxPoint = minAndMaxPoints [1];

     BoxCollider2D boxColl = gameObject.AddComponent<BoxCollider2D> ();
     boxColl.size = new Vector2 ((maxPoint.x - minPoint.x) + tetrimoPieces [0].GetColliderSize ().x, (maxPoint.y - minPoint.y) + tetrimoPieces [0].GetColliderSize ().x);
 }

 public void MoveTetrimoPiecesToCenter ()
 {
     List<Transform> tetrimoPiecesTransforms = new List<Transform> ();

     foreach (TetrimoPiece piece in tetrimoPieces) {
         tetrimoPiecesTransforms.Add (piece.transform);
     }

     Vector3 centroid = FindCentroid (tetrimoPiecesTransforms);

     foreach (Transform child in tetrimoPiecesTransforms) {
         child.position -= centroid;
     }
 }


 public Vector3[] GetMinAndMaxPoints (List<Transform> targets)
 {
     
     Vector3 minPoint = targets [0].position;
     Vector3 maxPoint = targets [0].position;

     for (int i = 1; i < targets.Count; i++) {

         Vector3 pos = targets [i].position;
         if (pos.x < minPoint.x)
             minPoint.x = pos.x;
         if (pos.x > maxPoint.x)
             maxPoint.x = pos.x;
         if (pos.y < minPoint.y)
             minPoint.y = pos.y;
         if (pos.y > maxPoint.y)
             maxPoint.y = pos.y;
         if (pos.z < minPoint.z)
             minPoint.z = pos.z;
         if (pos.z > maxPoint.z)
             maxPoint.z = pos.z;
     }


     Vector3[] minAndMaxPoints = new Vector3[]{ minPoint, maxPoint };
     return minAndMaxPoints;
 }


 //CALCULATING THE MEDIAN POINT BETWEEN ALL TETRIMOS
 private Vector3 FindCentroid (List< Transform > targets)
 {
     Vector3 centroid;
     Vector3[] minAndMaxPoints = GetMinAndMaxPoints (targets);
     Vector3 minPoint = minAndMaxPoints [0];
     Vector3 maxPoint = minAndMaxPoints [1];

     centroid = minPoint + 0.5f * (maxPoint - minPoint);
     return centroid;
 }

 public void FollowInput ()
 {
     Vector3 mouseScreenPos = Camera.main.ScreenToWorldPoint (Input.mousePosition);
     mouseScreenPos.z = originalPos.z;

     transform.position = mouseScreenPos;
 }

 public bool CanBeReleasedOverGrid ()
 {
     bool _canBeReleasedOverGrid = true;
     List<GridPiece> gridPiecesToFill = new List<GridPiece> ();

     foreach (TetrimoPiece piece in tetrimoPieces) {
         piece.SetNearestGridPiece ();
         gridPiecesToFill.Add (piece.lastNearestGridPiece);
     }
     foreach (GridPiece gridPiece in gridPiecesToFill) {
         if (gridPiece.isFilled || (TutorialManager.instance.isShowingTutorial && !gridPiece.isTutorialFillable)) {
             _canBeReleasedOverGrid = false;
             break;
         }
     }

     if (TetrimoPiecesShareSameNearestPiece ()) {
         _canBeReleasedOverGrid = false;
     }
     return _canBeReleasedOverGrid;
 }

 public bool TetrimoPiecesShareSameNearestPiece ()
 {

     foreach (TetrimoPiece tetrimoPiece in tetrimoPieces) {

         foreach (TetrimoPiece brotherPiece in tetrimoPieces) {
             bool isBrother = tetrimoPiece != brotherPiece;

             if (isBrother) {                    
                 if (tetrimoPiece.lastNearestGridPiece == brotherPiece.lastNearestGridPiece) {
                     return true;
                 }
             }

         }

     }

     return false;
 }

 public void FillNearestGridPieces ()
 {
     foreach (TetrimoPiece tetrimoPiece in tetrimoPieces) {
         tetrimoPiece.lastNearestGridPiece.SetFilled (true);
         tetrimoPiece.lastNearestGridPiece.SetTetrimoThatFillsMe (this);
     }
 }



 public void PlaceOverGrid ()
 {
     FillNearestGridPieces ();
     if (!TutorialManager.instance.isShowingTutorial) {
         ScoreHandler.instance.increaseScore (tetrimoPieces.Count ());
     }
     DestroyImmediate (gameObject);
 }



 public void MoveToSpawnPosition ()
 {
     transform.position = originalSpawnPos;
 }

 public bool CanFitInGrid ()
 {
     GridPiece[] gridPieces = Grid.instance.gridPieces.ToArray ();
     bool canFitInGrid = false;

     Vector3 scaleBeforeCheck = transform.localScale;
     Vector3 positionBeforeCheck = transform.position;

     foreach (GridPiece gridPiece in gridPieces) {

         if (gridPiece != null)
         {
             transform.localScale = originalScale;
             transform.position = gridPiece.transform.position;
             transform.position += new Vector3(transform.position.x - tetrimoPieces[0].transform.position.x, transform.position.y - tetrimoPieces[0].transform.position.y, transform.position.z);

             if (CanBeReleasedOverGrid())
             {
                 canFitInGrid = true;
                 break;
             }
         }

     }
         
     transform.position = positionBeforeCheck;
     transform.localScale = scaleBeforeCheck;

     return canFitInGrid;
 }


 public void ReturnToSpawnPoint (float timeToReachOriginalPos)
 {

     if (OnReturnToSpawnPointRoutine == null) {
         OnReturnToSpawnPointRoutine = DOOnReturnToSpawnPointRoutine (timeToReachOriginalPos);
         StartCoroutine (OnReturnToSpawnPointRoutine);
     }
 }

 public IEnumerator OnReturnToSpawnPointRoutine;

 public IEnumerator DOOnReturnToSpawnPointRoutine (float timeToReachOriginalPos, bool animated = true)
 {
     
     float timeToReachValues = timeToReachOriginalPos;

     float t = 0;
     float timePassed = 0;

     Vector3 startPos = transform.position;
     Vector3 startScale = transform.localScale;
     Vector3 targetScale = originalScale * TetrimoSpawner.instance.scaleFactorOnSpawnPoint;

     if (!animated) {
         t = 1;
     }

     while (t <= 1) {

         if (this == null || isBeingDragged) {
             yield break;
         }


         transform.localScale = Vector3.Lerp (startScale, targetScale, t);
         transform.position = Vector3.Lerp (startPos, originalSpawnPos, t);

         t += Time.deltaTime / timeToReachValues;
         timePassed += Time.deltaTime;

         if (t >= 1) {
             
             transform.localScale = targetScale;
             transform.position = originalSpawnPos;

         }

         yield return new WaitForEndOfFrame ();
     }
         

     OnReturnToSpawnPointRoutine = null;
 }



 public void OnDrag ()
 {

     if (OnDragRoutine == null) {
         isBeingDragged = true;
         OnDragRoutine = DOOnDragRoutine ();

         if (OnReturnToSpawnPointRoutine != null) {
             StopCoroutine (OnReturnToSpawnPointRoutine);
         }

         StartCoroutine (OnDragRoutine);
     }
 }

 public void StopDrag ()
 {
     if (OnDragRoutine != null) {
         isBeingDragged = false;
         StopCoroutine (OnDragRoutine);
         OnDragRoutine = null;
     }
 }


 public IEnumerator OnDragRoutine;

 public IEnumerator DOOnDragRoutine ()
 {
     float timeToReachValues = 0.1f;

     float t = 0;
     float timePassed = 0;

     Vector3 startPos = transform.position;
     Vector3 startScale = transform.localScale;

     while (t <= 1 && isBeingDragged) {
         
         transform.localScale = Vector3.Lerp (startScale, originalScale, t);
         transform.position = Vector3.Lerp (startPos, GetFollowMousePos (), t);

         t += Time.deltaTime / timeToReachValues;
         timePassed += Time.deltaTime;

         if (t >= 1) {
             transform.localScale = originalScale;
         }

         yield return new WaitForEndOfFrame ();
     }

     while (isBeingDragged) {
         transform.position = GetFollowMousePos ();
         yield return new WaitForEndOfFrame ();
     }

 }

 public Vector3 GetFollowMousePos ()
 {
     float distanceBetweenCenterAndTetrimoMinPoint = Mathf.Abs (_centroid.y - _minAndMaxPoints [0].y);

     Vector3 worldMousePos = Camera.main.ScreenToWorldPoint (Input.mousePosition);
     worldMousePos.z = transform.position.z;
     worldMousePos.y += distanceBetweenCenterAndTetrimoMinPoint + 1.15f;

     return worldMousePos;
 }

 public void OnDisable ()
 {
     Vector3 targetScale = originalScale * TetrimoSpawner.instance.scaleFactorOnSpawnPoint;

     transform.position = originalSpawnPos;
     transform.localScale = targetScale;
 }


and here is the code that is attached to every square pieces that makes up the prefab:

 public BoxCollider2D coll;
 public GridPiece lastNearestGridPiece;
 private SpriteRenderer _spriteRenderer;

 public int SortingOrder {
     get {
         return spriteRenderer.sortingOrder;
     }
     set {
         spriteRenderer.sortingOrder = value;
     }
 }

 public SpriteRenderer spriteRenderer {
     get {
         if (_spriteRenderer == null) {
             _spriteRenderer = GetComponentInChildren<SpriteRenderer> ();
         }
         return _spriteRenderer;
     }
 }

 void Awake ()
 {
     coll = GetComponentInChildren<BoxCollider2D> ();
 }

 // Use this for initialization
 void Start ()
 {
 }
 
 // Update is called once per frame
 void Update ()
 {
     
 }

 public Vector2 GetColliderSize ()
 {
     return coll.bounds.size;
 }

 public void SetNearestGridPiece ()
 {
     
     GridPiece[] gridPieces = Grid.instance.gridPieces.ToArray ();

     GridPiece lastNearestGridPieceFound = gridPieces[0];

     float lastNearestDistanceFound = Vector3.Distance (gridPieces [0].transform.position, transform.position);

     foreach (GridPiece piece in gridPieces) {
         float distanceBetweenGridPieceAndThis = Vector3.Distance(piece.transform.position, transform.position);

         if (distanceBetweenGridPieceAndThis < lastNearestDistanceFound)
         {
             lastNearestGridPieceFound = piece;
             lastNearestDistanceFound = distanceBetweenGridPieceAndThis;
         }
     }

     lastNearestGridPiece = lastNearestGridPieceFound;
 }

 public void SetColor (Color color)
 {
     spriteRenderer.color = color;
 }

here is the code for generating grid: public static Grid instance;

 public int rowsCount;
 public int columnsCount;

 public GridPiecesSet[] rows;
 public GridPiecesSet[] columns;

 public List<GridPiece> gridPieces;

 public float distanceBetweenGroundPieces;
 public GridPiece gridPiece;

 GridPiecesContainer gridPiecesContainer;

 void Awake ()
 {
     instance = this;    
     gridPiecesContainer = GetComponentInChildren<GridPiecesContainer> ();
     GenerateGrid ();
 }

 // Use this for initialization
 void Start ()
 {
 }
 
 // Update is called once per frame
 void Update ()
 {
     
 }

 //This generates the grid
 public void GenerateGrid ()
 {
     rows = Enumerable.Range (0, columnsCount).Select (i => new GridPiecesSet (columnsCount, GridPiecesSet.Type.row)).ToArray ();
     columns = Enumerable.Range (0, rowsCount).Select (i => new GridPiecesSet (rowsCount, GridPiecesSet.Type.column)).ToArray ();

     gridPieces = new List<GridPiece> ();

     for (int rowsIndex = 0; rowsIndex < rowsCount; rowsIndex++) {
         GenerateRow (rowsIndex);
     }

     gridPiecesContainer.MoveToCenter ();

 }

 void GenerateRow (int rowsIndex)
 {
     GridPiece lastSpawnedRowPiece = null;

     for (int columnsIndex = 0; columnsIndex < columnsCount; columnsIndex++) {
         if (rowsIndex == 0 && columnsIndex == 4 || columnsIndex == 5)
         {
             GridPiece newGridPiece = Instantiate(gridPiece.gameObject).GetComponent<GridPiece>();
             newGridPiece.transform.SetParent(gridPiecesContainer.transform);

             Vector3 newPos = newGridPiece.transform.position;

             // GENERATING FROM TOP LEFT
             newPos.x = (newGridPiece.GetWidth() / 2 + distanceBetweenGroundPieces) * columnsIndex;
             newPos.y = (newGridPiece.GetHeight() / 2 + distanceBetweenGroundPieces) * (-rowsIndex);

             newGridPiece.transform.position = newPos;
             newGridPiece.name = " piece_row_" + rowsIndex + " piece_col_" + columnsIndex;

             rows[rowsIndex].gridPieces[columnsIndex] = newGridPiece;
             columns[columnsIndex].gridPieces[rowsIndex] = newGridPiece;

             gridPieces.Add(newGridPiece);


             lastSpawnedRowPiece = newGridPiece.GetComponent<GridPiece>();
         }
         else if ((columnsIndex >= 3 && columnsIndex <= 6) && rowsIndex == 1)
         {
             GridPiece newGridPiece = Instantiate(gridPiece.gameObject).GetComponent<GridPiece>();
             newGridPiece.transform.SetParent(gridPiecesContainer.transform);

             Vector3 newPos = newGridPiece.transform.position;

             // GENERATING FROM TOP LEFT
             newPos.x = (newGridPiece.GetWidth() / 2 + distanceBetweenGroundPieces) * columnsIndex;
             newPos.y = (newGridPiece.GetHeight() / 2 + distanceBetweenGroundPieces) * (-rowsIndex);

             newGridPiece.transform.position = newPos;
             newGridPiece.name = " piece_row_" + rowsIndex + " piece_col_" + columnsIndex;

             rows[rowsIndex].gridPieces[columnsIndex] = newGridPiece;
             columns[columnsIndex].gridPieces[rowsIndex] = newGridPiece;

             gridPieces.Add(newGridPiece);


             lastSpawnedRowPiece = newGridPiece.GetComponent<GridPiece>();
         }
         else if ((columnsIndex >= 2 && columnsIndex <= 7) && rowsIndex == 2)
         {
             GridPiece newGridPiece = Instantiate(gridPiece.gameObject).GetComponent<GridPiece>();
             newGridPiece.transform.SetParent(gridPiecesContainer.transform);

             Vector3 newPos = newGridPiece.transform.position;

             // GENERATING FROM TOP LEFT
             newPos.x = (newGridPiece.GetWidth() / 2 + distanceBetweenGroundPieces) * columnsIndex;
             newPos.y = (newGridPiece.GetHeight() / 2 + distanceBetweenGroundPieces) * (-rowsIndex);

             newGridPiece.transform.position = newPos;
             newGridPiece.name = " piece_row_" + rowsIndex + " piece_col_" + columnsIndex;

             rows[rowsIndex].gridPieces[columnsIndex] = newGridPiece;
             columns[columnsIndex].gridPieces[rowsIndex] = newGridPiece;

             gridPieces.Add(newGridPiece);


             lastSpawnedRowPiece = newGridPiece.GetComponent<GridPiece>();
         }
         else if ((columnsIndex >= 1 && columnsIndex <= 8) && rowsIndex == 3)
         {

             GridPiece newGridPiece = Instantiate(gridPiece.gameObject).GetComponent<GridPiece>();
             newGridPiece.transform.SetParent(gridPiecesContainer.transform);

             Vector3 newPos = newGridPiece.transform.position;

             // GENERATING FROM TOP LEFT
             newPos.x = (newGridPiece.GetWidth() / 2 + distanceBetweenGroundPieces) * columnsIndex;
             newPos.y = (newGridPiece.GetHeight() / 2 + distanceBetweenGroundPieces) * (-rowsIndex);

             newGridPiece.transform.position = newPos;
             newGridPiece.name = " piece_row_" + rowsIndex + " piece_col_" + columnsIndex;

             rows[rowsIndex].gridPieces[columnsIndex] = newGridPiece;
             columns[columnsIndex].gridPieces[rowsIndex] = newGridPiece;

             gridPieces.Add(newGridPiece);


             lastSpawnedRowPiece = newGridPiece.GetComponent<GridPiece>();
         }
         else if (rowsIndex == 4)
         {
             GridPiece newGridPiece = Instantiate(gridPiece.gameObject).GetComponent<GridPiece>();
             newGridPiece.transform.SetParent(gridPiecesContainer.transform);

             Vector3 newPos = newGridPiece.transform.position;

             // GENERATING FROM TOP LEFT
             newPos.x = (newGridPiece.GetWidth() / 2 + distanceBetweenGroundPieces) * columnsIndex;
             newPos.y = (newGridPiece.GetHeight() / 2 + distanceBetweenGroundPieces) * (-rowsIndex);

             newGridPiece.transform.position = newPos;
             newGridPiece.name = " piece_row_" + rowsIndex + " piece_col_" + columnsIndex;

             rows[rowsIndex].gridPieces[columnsIndex] = newGridPiece;
             columns[columnsIndex].gridPieces[rowsIndex] = newGridPiece;

             gridPieces.Add(newGridPiece);


             lastSpawnedRowPiece = newGridPiece.GetComponent<GridPiece>();
         }
         else if (rowsIndex == 5)
         {
             GridPiece newGridPiece = Instantiate(gridPiece.gameObject).GetComponent<GridPiece>();
             newGridPiece.transform.SetParent(gridPiecesContainer.transform);

             Vector3 newPos = newGridPiece.transform.position;

             // GENERATING FROM TOP LEFT
             newPos.x = (newGridPiece.GetWidth() / 2 + distanceBetweenGroundPieces) * columnsIndex;
             newPos.y = (newGridPiece.GetHeight() / 2 + distanceBetweenGroundPieces) * (-rowsIndex);

             newGridPiece.transform.position = newPos;
             newGridPiece.name = " piece_row_" + rowsIndex + " piece_col_" + columnsIndex;

             rows[rowsIndex].gridPieces[columnsIndex] = newGridPiece;
             columns[columnsIndex].gridPieces[rowsIndex] = newGridPiece;

             gridPieces.Add(newGridPiece);


             lastSpawnedRowPiece = newGridPiece.GetComponent<GridPiece>();
         }
         else if ((columnsIndex >= 1 && columnsIndex <= 8) && rowsIndex == 6)
         {
             GridPiece newGridPiece = Instantiate(gridPiece.gameObject).GetComponent<GridPiece>();
             newGridPiece.transform.SetParent(gridPiecesContainer.transform);

             Vector3 newPos = newGridPiece.transform.position;

             // GENERATING FROM TOP LEFT
             newPos.x = (newGridPiece.GetWidth() / 2 + distanceBetweenGroundPieces) * columnsIndex;
             newPos.y = (newGridPiece.GetHeight() / 2 + distanceBetweenGroundPieces) * (-rowsIndex);

             newGridPiece.transform.position = newPos;
             newGridPiece.name = " piece_row_" + rowsIndex + " piece_col_" + columnsIndex;

             rows[rowsIndex].gridPieces[columnsIndex] = newGridPiece;
             columns[columnsIndex].gridPieces[rowsIndex] = newGridPiece;

             gridPieces.Add(newGridPiece);


             lastSpawnedRowPiece = newGridPiece.GetComponent<GridPiece>();
         }
         else if ((columnsIndex >= 2 && columnsIndex <= 7) && rowsIndex == 7)
         {
             GridPiece newGridPiece = Instantiate(gridPiece.gameObject).GetComponent<GridPiece>();
             newGridPiece.transform.SetParent(gridPiecesContainer.transform);

             Vector3 newPos = newGridPiece.transform.position;

             // GENERATING FROM TOP LEFT
             newPos.x = (newGridPiece.GetWidth() / 2 + distanceBetweenGroundPieces) * columnsIndex;
             newPos.y = (newGridPiece.GetHeight() / 2 + distanceBetweenGroundPieces) * (-rowsIndex);

             newGridPiece.transform.position = newPos;
             newGridPiece.name = " piece_row_" + rowsIndex + " piece_col_" + columnsIndex;

             rows[rowsIndex].gridPieces[columnsIndex] = newGridPiece;
             columns[columnsIndex].gridPieces[rowsIndex] = newGridPiece;

             gridPieces.Add(newGridPiece);


             lastSpawnedRowPiece = newGridPiece.GetComponent<GridPiece>();
         }
         else if ((columnsIndex >= 3 && columnsIndex <= 6) && rowsIndex == 8)
         {
             GridPiece newGridPiece = Instantiate(gridPiece.gameObject).GetComponent<GridPiece>();
             newGridPiece.transform.SetParent(gridPiecesContainer.transform);

             Vector3 newPos = newGridPiece.transform.position;

             // GENERATING FROM TOP LEFT
             newPos.x = (newGridPiece.GetWidth() / 2 + distanceBetweenGroundPieces) * columnsIndex;
             newPos.y = (newGridPiece.GetHeight() / 2 + distanceBetweenGroundPieces) * (-rowsIndex);

             newGridPiece.transform.position = newPos;
             newGridPiece.name = " piece_row_" + rowsIndex + " piece_col_" + columnsIndex;

             rows[rowsIndex].gridPieces[columnsIndex] = newGridPiece;
             columns[columnsIndex].gridPieces[rowsIndex] = newGridPiece;

             gridPieces.Add(newGridPiece);


             lastSpawnedRowPiece = newGridPiece.GetComponent<GridPiece>();
         }
         else if ((columnsIndex >= 4 && columnsIndex <= 5) && rowsIndex == 9)
         {
             GridPiece newGridPiece = Instantiate(gridPiece.gameObject).GetComponent<GridPiece>();
             newGridPiece.transform.SetParent(gridPiecesContainer.transform);

             Vector3 newPos = newGridPiece.transform.position;

             // GENERATING FROM TOP LEFT
             newPos.x = (newGridPiece.GetWidth() / 2 + distanceBetweenGroundPieces) * columnsIndex;
             newPos.y = (newGridPiece.GetHeight() / 2 + distanceBetweenGroundPieces) * (-rowsIndex);

             newGridPiece.transform.position = newPos;
             newGridPiece.name = " piece_row_" + rowsIndex + " piece_col_" + columnsIndex;

             rows[rowsIndex].gridPieces[columnsIndex] = newGridPiece;
             columns[columnsIndex].gridPieces[rowsIndex] = newGridPiece;

             gridPieces.Add(newGridPiece);


             lastSpawnedRowPiece = newGridPiece.GetComponent<GridPiece>();
         }
     }
 }

 public bool IsGameOver ()
 {

     bool gameOver = true;

     foreach (Tetrimo tetrimo in TetrimoSpawner.instance.spawnedTetrimos) {
         
         if (tetrimo != null) {
             if (tetrimo.CanFitInGrid ()) {
                 gameOver = false;
                 break;
             }
         }

     }            

     return gameOver;
 }

 public void CleanCompletedRowsAndColumns ()
 {
     
     List<GridPiecesSet> rowsAndColumnsToClean = new List<GridPiecesSet> ();

     foreach (GridPiecesSet row in rows) {
         if (row.AllPiecesFilled () && row.colorMatch()) {
             rowsAndColumnsToClean.Add (row);
         }
     }

     foreach (GridPiecesSet column in columns) {
         if (column.AllPiecesFilled () && column.colorMatch()) {
             rowsAndColumnsToClean.Add (column);
         }
     }

     if (rowsAndColumnsToClean.Count > 0) {
         ClearAllCompletedRowsAndColumns (rowsAndColumnsToClean);
     }

 }

 public void ClearAllCompletedRowsAndColumns (List<GridPiecesSet> rowsAndColumnsToClean)
 {
     
     SoundsManager.instance.PlayLineCompleted ();

     foreach (GridPiecesSet pieceSet in rowsAndColumnsToClean) {
         pieceSet.Clean ();
         StartCoroutine (DOCleanAnimation (pieceSet));
     }

 }

 public void ClearGrid ()
 {
     foreach (GridPiece gridPiece in gridPieces) {
         gridPiece.SetFilled (false);
     }
 }

 public GridPieceConfiguration[] GetCurrentGridConfiguration ()
 {

     GridPieceConfiguration[] gridConfig = new GridPieceConfiguration[gridPieces.Count ()];
         
     int count = 0;
     foreach (GridPiece gridPiece in gridPieces) {
         gridConfig [count] = new GridPieceConfiguration ();
         gridConfig [count].ActivationState = gridPiece.isFilled;
         gridConfig [count].TetrimoThatFillsName = gridPiece.tetrimoPieceThatFillsMeName;
         count++;
     }

     return gridConfig;
 }

 public void TurnOffAllTutorialFillablePieces ()
 {
     foreach (GridPiece piece in gridPieces) {
         piece.SetIlluminated (false);
         piece.SetTutorialFillable (false);
     }
 }

 public void SetGridByConfiguration (GridPieceConfiguration[] configs)
 {
     
     int count = 0;
     foreach (GridPieceConfiguration config in configs) {

         GridPiece iteratedPiece = gridPieces [count];

         if (config.ActivationState == true) {
             iteratedPiece.SetFilled (true);

             Tetrimo tetrimoThatFillsGridPiece = Resources.Load<Tetrimo> ("Tetrimos/" + config.TetrimoThatFillsName);
             if (tetrimoThatFillsGridPiece == null) {
                 throw new UnassignedReferenceException ();
             }
             iteratedPiece.SetTetrimoThatFillsMe (tetrimoThatFillsGridPiece);


         } else {
             iteratedPiece.SetFilled (false);
         }
             
         iteratedPiece.SetIlluminated (false);
         iteratedPiece.SetTutorialFillable (true);

         count++;
     }
 }


 public IEnumerator DOCleanAnimation (GridPiecesSet piecesSet)
 {

     List<GridPiece> gridPiecesToAnimate = new List<GridPiece> ();

     foreach (GridPiece piece in piecesSet.gridPieces) {
         if (piece != null)
         {
             GridPiece clonePiece = GameObject.Instantiate(piece.gameObject).GetComponent<GridPiece>();
             clonePiece.transform.position = piece.transform.position;
             clonePiece.graphicWhenFilled.sortingOrder += 10;
             clonePiece.SetFilled(true);
             gridPiecesToAnimate.Add(clonePiece);
         }
     }

     foreach (GridPiece piece in gridPiecesToAnimate) {
         if (piece != null)
         {
             StartCoroutine(DisappearAnim(piece, piecesSet));
             yield return new WaitForSeconds(0.07f);
         }
     }

     yield return new WaitForEndOfFrame ();
 }


 IEnumerator DisappearAnim (GridPiece piece, GridPiecesSet piecesSet)
 {

     float t = 0;
     float animTime = 0.15f;

     Vector3 startScale = piece.transform.localScale;
     Vector3 targetScale = Vector3.zero;

     while (t <= 1) {

         piece.transform.localScale = Vector3.Lerp (startScale, targetScale, t);

         t += Time.deltaTime / animTime;
         yield return new WaitForEndOfFrame ();

         if (t >= 1) {
             piece.transform.localScale = targetScale;
         }
     }

     piece.DestroySelf ();
 }

and finally the inputhandler that handles input from mouse or touch

     bool isDragging = false;
 Tetrimo tetrimoToMove = null;

 // Use this for initialization
 void Start ()
 {
 }


 void Update ()
 {

     if (Input.GetMouseButtonDown (0)) {
         OnMouseButtonDown ();
     }

     if (Input.GetMouseButtonUp (0)) {
         OnMouseButtonUp ();
     }

     if (isDragging) {
         OnDrag ();
     }


 }

 public void OnDrag ()
 {
     tetrimoToMove.OnDrag ();
 }

 public void OnMouseButtonUp ()
 {
     if (isDragging) {
         
         if (tetrimoToMove.CanBeReleasedOverGrid ()) {                        
             Debug.Log("Mouse Up");
             if (TutorialManager.instance.isShowingTutorial) {
                 StartCoroutine (TutorialManager.instance.OnTetrimoReleasedOverGrid ());
             }

             SoundsManager.instance.PlayTetrimoPlacement ();
             tetrimoToMove.PlaceOverGrid ();
             Grid.instance.CleanCompletedRowsAndColumns ();

             if (Grid.instance.IsGameOver () && !TetrimoSpawner.instance.HasToSpawnNewTetrimos () && !TutorialManager.instance.isShowingTutorial) {
                 StartCoroutine(ObliusGameManager.instance.GameOver ());
                 Debug.Log ("GAME OVER");
             } else {
                 Debug.Log ("GAME CAN CONTINUE");
             }

             if (!TutorialManager.instance.isShowingTutorial) {
                 GameStateSerializer.SaveCurrentState (Grid.instance, TetrimoSpawner.instance, ScoreHandler.instance);
             }

             if (!TutorialManager.instance.isShowingTutorial && TetrimoSpawner.instance.HasToSpawnNewTetrimos()) {                    
                 StartCoroutine (TetrimoSpawner.instance.TetrimoSpawnRoutine (true));    
             }

         } else {                        
             tetrimoToMove.StopDrag ();
             tetrimoToMove.ReturnToSpawnPoint (0.1f);
         }
     }

     isDragging = false;
 }

 public void OnMouseButtonDown ()
 {
     if (!isDragging && ObliusGameManager.instance.gameState != ObliusGameManager.GameState.gameover) {
         tetrimoToMove = GetClickedTetrimo ();

         if (tetrimoToMove != null) {

             if (TutorialManager.instance.isShowingTutorial) {

                 if (tetrimoToMove.assignedSpawnPoint != TutorialManager.instance.currentShownTutorialStep.clickableSpawnPoint) {
                     isDragging = false;
                     return;
                 }

                 TutorialManager.instance.tutorialhand.StopMoveAndHide ();
             }
             isDragging = true;

         } else {
             isDragging = false;
         }
     }
 }


 public Tetrimo GetClickedTetrimo ()
 {

     Tetrimo clickedTetrimo = null;

     Vector3 mousePos = Input.mousePosition;
     mousePos.z = 10;

     Vector3 screenPos = Camera.main.ScreenToWorldPoint (mousePos);
     RaycastHit2D hit = Physics2D.Raycast (screenPos, Vector2.zero);
     if (hit) {

         Tetrimo hitTetrimo = hit.transform.GetComponentInParent<Tetrimo> ();
         TetrimoSpawnPoint hitSpawnPoint = hit.transform.GetComponent<TetrimoSpawnPoint> ();

         if (hitTetrimo != null) {
             clickedTetrimo = hitTetrimo;
         } else if (hitSpawnPoint != null) {
             clickedTetrimo = hitSpawnPoint.assignedTetrimo;
             Debug.Log ("Spawn point hit");
         }
     } 

     return clickedTetrimo;
 }

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
â–¼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image NorthStar79 · Dec 14, 2017 at 05:28 AM 0
Share

this question was marked as spam by the system because length of your codes. i accepted it and published, but i suggest you to modify your codes to simpler version. clear out any other code that not cause this error. by this way you can have a greater chance to get your answer. as you can guess, most of the people won't want to read all this code. and skip over it. just a suggestion.

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

150 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Vector3.Distance returning offset values due to down force interference. 1 Answer

Grid Movement with Smooth Movement 0 Answers

Creating a Grid using an Array with C# 1 Answer

The object moves more that what I want (MoveTowards) 0 Answers

Creating a grid that holds objects of different sizes? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges